头文件中加入
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);
}
本文介绍如何在Qt应用程序中使用QPushButton和QLabel控件,并通过槽函数editTextSlot响应按钮点击事件,动态修改QLabel的文字颜色。具体实现包括设置QPushButton的位置、大小和文字,创建QLabel并设置其初始文字,然后利用QColorDialog选择颜色,通过QPalette更改QLabel的颜色。
1792

被折叠的 条评论
为什么被折叠?



