This is my 16th day of the August Genwen Challenge

@[TOC]


The effect


code

Create four sliders that look like this:

Set the maximum value for each slider to 255 because RGB is only 255 for each parameter.! [Insert picture description here]Img – blog. Csdnimg. Cn / 20200527174…=400x) then create four labels and a TextEdit to display text and color backplates:Here’s the code: Right-click slider to go to the slot and select valueChanged(int) to add the following code

    Q_UNUSED(value);
    QColor color;
    // Get the values of the four scroll bars
    int R = ui->verticalSlider->value(a);int G = ui->verticalSlider_2->value(a);int B = ui->verticalSlider_3->value(a);int A = ui->verticalSlider_4->value(a);/ / output debugging information qDebug () < < "R =" < < < < R "G =" < < < < G "B =" < < < < < < "A =" B A;
    color.setRgb(R,G,B,A);
    QString strClr = color.name(a);// "#XXXXX" strClr will get data in hexadecimal color
    QString strStyleSheet = QString("background-color: %1").arg( strClr ); //%1 uses arG concatenation strings similar to placeholders in printf()
    ui->textEdit->setStyleSheet( strStyleSheet ); // Change the textEdit stylesheet
	
Copy the code

So you can see the color in the preview, but this is just the slot code for one of the sliders, and then we’ll associate the other three.

// Add the following code to the constructor
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   // Add the code
    QObject::connect(ui->verticalSlider_2,SIGNAL(valueChanged(int)),this.SLOT(on_verticalSlider_valueChanged(int)));
    QObject::connect(ui->verticalSlider_3,SIGNAL(valueChanged(int)),this.SLOT(on_verticalSlider_valueChanged(int)));
    QObject::connect(ui->verticalSlider_4,SIGNAL(valueChanged(int)),this.SLOT(on_verticalSlider_valueChanged(int)));
    // Attach valueChanged(int) signals from different sliders to the first slot function, so that any of the four values that change send signals to call the slot function.
}
Copy the code

What does this palette do? You can use it to set background colors.

To add the above code to the button’s slot function, simply change textEdit to Widget UI -> Widget ->setStyleSheet(strStyleSheet); Can.