Спасибо, отвечая, я обновляю свой код не все, я удаляю некоторые функции, только передние / задние настройки цвета. Я пытаюсь получить 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
}