头文件中加入
private:
Ui::MainWindow *ui;
QPushButton *pushButton;
QLabel *label;
private slots:
void editTextSlot();
构造函数中:
pushButton = new QPushButton(this);
pushButton->setGeometry(QRect(160, 100, 50, 30));
pushButton->setText("选择颜色");
label = new QLabel(this);
label->setGeometry(QRect(50, 100, 100, 30));
label->setText("我的颜色可以改变");
connect(pushButton, &QPushButton::clicked, this, &MainWindow::editTextSlot);
槽函数:
void MainWindow::editTextSlot()
{
//set QColorDialog
QColorDialog::setCustomColor(0, QRgb(0x0000ff));
//定义QColor
QColor color = QColorDialog::getColor(QColor(0, 255, 0));
//定义QPalette(调色板类)
QPalette p = palette();
//调色板接收颜色
p.setColor(QPalette::WindowText, QColor(color));
//给labe绑定颜色
label->setPalette(p);
}