QColorDialog :: getColor函数获取RGB值并转换为十六进制

时间:2018-12-09 07:31:31

标签: c++ qt qt5

感谢回答我不是全部更新代码,而是仅删除正面/背面颜色设置的某些功能 我尝试获取QColorDialog::getColor的RGB值并转换为十六进制 以下是建议和更新后的代码:

"mainwindow.cpp"

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColorDialog>
#include <QColor>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow),
    m_serial(new QSerialPort(this))

{
    m_ui->setupUi(
    m_ui->ClearScreen_Button->setEnabled(false);
    m_ui->SendFrontColor_Button->setEnabled(false);
    m_ui->SendBackColor_Button->setEnabled(false);
    m_ui->SetFrontColor_Button->setEnabled(false);
    m_ui->SetBackColor_Button->setEnabled(false);
    //
    fc.clear();
    fc.append(0xAA);
    fc.append(0x02);
    fc.append(0x03);
    fc.append(0xFF);
    fc.append(0xFF);
    fc.append(0xFF);
    fc.append(0xC3);
    bc.clear();
    bc.append(0xAA);
    bc.append(0x03);
    bc.append(0x03);
    bc.append(static_cast<char>(0x00));
    bc.append(static_cast<char>(0x00));
    bc.append(static_cast<char>(0x00));
    bc.append(0xC3);

}


MainWindow::~MainWindow()
{
    delete m_ui;
}

void MainWindow::openSerialPort()
{

    if (m_serial->open(QIODevice::ReadWrite))
    {
        //QByteArray b;
        //b.clear();
        //b.append(0xAA);
        //b.append(0x20);
        //b.append(0x01);
        //b.append(static_cast<char>(0x00));
        //b.append(0xC3);
        //m_serial->write(b);
        }//end if
}

void MainWindow::closeSerialPort()
{
    if (m_serial->isOpen())
        m_serial->close();

}


void MainWindow::on_ClearScreen_Button_clicked()
{
    fc.clear();
    fc.append(0xAA);
    fc.append(0x02);
    fc.append(0x03);
    fc.append(0xFF);
    fc.append(0xFF);
    fc.append(0xFF);
    fc.append(0xC3);
    bc.clear();
    bc.append(0xAA);
    bc.append(0x03);
    bc.append(0x03);
    bc.append(static_cast<char>(0x00));
    bc.append(static_cast<char>(0x00));
    bc.append(static_cast<char>(0x00));
    bc.append(0xC3);
    //
    QByteArray b;
    b.clear();
    b.append(0xAA);
    b.append(0x02);
    b.append(static_cast<char>(0x00));
    b.append(0xC3);
    m_serial->write(b);
}

void MainWindow::on_SendFrontColor_Button_clicked()
{
    m_serial->write(fc);

}

void MainWindow::on_SendBackColor_Button_clicked()
{
    m_serial->write(bc);
}

void MainWindow::on_SetFrontColor_Button_clicked()
{
    FrontColor=QColorDialog::getColor(Qt::white,this,"Choose Front Color",QColorDialog::DontUseNativeDialog);
    if(FrontColor.isValid())
    {
        fc.clear();
        fc.append(0xAA);
        fc.append(0x02);
        fc.append(0x03);
        fc.append(0xFF);
        fc.append(0xFF);
        fc.append(0xFF);
        fc.append(0xC3);

        QRgb rgb = FrontColor.rgb();
        fc[5] = qRed(rgb);
        fc[4] = qGreen(rgb);
        fc[3] = qBlue(rgb);
    }//end if
}

void MainWindow::on_SetBackColor_Button_clicked()
{
    BackColor=QColorDialog::getColor(Qt::white,this,"Choose Back Color",QColorDialog::DontUseNativeDialog);
    if(BackColor.isValid())
    {
        bc.clear();
        bc.append(0xAA);
        bc.append(0x03);
        bc.append(0x03);
        bc.append(static_cast<char>(0x00));
        bc.append(static_cast<char>(0x00));
        bc.append(static_cast<char>(0x00));
        bc.append(0xC3);

        QRgb rgb = BackColor.rgb();
        bc[5] = qRed(rgb);
        bc[4] = qGreen(rgb);
        bc[3] = qBlue(rgb);
    }//end if
}

1 个答案:

答案 0 :(得分:0)

您对十六进制值和可动字符感到困惑。

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow),
    m_serial(new QSerialPort(this))
{
    m_ui->setupUi(this);
    QPalette frontpalette = m_ui->label_FrontColor->palette();
    frontpalette.setColor(m_ui->label_FrontColor->backgroundRole(), Qt::white);
    m_ui->label_FrontColor->setAutoFillBackground(true);
    m_ui->label_FrontColor->setPalette(frontpalette);
//    fc=("AA 02 03 FF FF FF C3");
    fc.clear();
    fc.append(static_cast<char>(0xAA));
    fc.append(static_cast<char>(0x02));
    fc.append(static_cast<char>(0x03));
    fc.append(static_cast<char>(0xFF));
    fc.append(static_cast<char>(0xFF));
    fc.append(static_cast<char>(0xFF));
    fc.append(static_cast<char>(0xC3));
}

MainWindow::~MainWindow()
{
    delete m_ui;
}
void MainWindow::on_SetFrontColor_Button_clicked()
{
    FrontColor=QColorDialog::getColor(Qt::white,this,"Choose Front Color",QColorDialog::DontUseNativeDialog);
    if(FrontColor.isValid())
    {
        QPalette palette = m_ui->label_FrontColor->palette();
        palette.setColor(m_ui->label_FrontColor->backgroundRole(), FrontColor);
        m_ui->label_FrontColor->setAutoFillBackground(true);
        m_ui->label_FrontColor->setPalette(palette);
        QRgb rgb = FrontColor.rgb();
        fc[3] = static_cast<char>(qRed(rgb));
        fc[4] = static_cast<char>(qGreen(rgb));
        fc[5] = static_cast<char>(qBlue(rgb));
    }//end if
}

void MainWindow::on_SendFrontColor_Button_clicked()
{
    m_serial->write(fc);
}