您的位置:首页 > 编程语言 > Qt开发

Qt标准对话框之QColorDialog

2017-05-20 23:43 253 查看
在mainwindow.h MainWindow类中添加

public  slots:
void colorDlg();


构造函数

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(600,600);
QPushButton *pb1 = new QPushButton("颜色",this);
pb1->move(150,50);
connect(pb1,&QPushButton::clicked,this,MainWindow::colorDlg);
}


colorDlg的两种写法:

第一种:

void MainWindow::colorDlg()
{
//增加QColorDialog::ShowAlphaChannel 可设置透明度
QColor color = QColorDialog::getColor(Qt::red,this,tr("颜色对话框"),QColorDialog::ShowAlphaChannel);
//若点击了对话框的取消则得到非法颜色
if(color.isValid())
{
//将得到的颜色作为窗口背景色
QPalette qp(this->palette());
qp.setColor(QPalette::Background,color);
this->setAutoFillBackground(true);
this->setPalette(qp);
}
}


第二种:

void MainWindow::colorDlg()
{
//通过建立QColorDialog比较灵活
QColorDialog cd;
cd.setOption(QColorDialog::ShowAlphaChannel);
cd.exec();
QColor color = cd.currentColor();
if(color.isValid())
{
QPalette qp(this->palette());
qp.setColor(QPalette::Background,color);
this->setAutoFillBackground(true);
this->setPalette(qp);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  对话框 qt qt5