1、新建"QT GUI",基类选择"QDialog',取消“创建界面”
2、dialog.cpp中:
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
#include
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private: //私有变量声明
QPushButton *colorBtn;
QFrame *colorFrame;
QGridLayout *mainLayout;
private slots:
void showColor();
};
#endif // DIALOG_H
3、dialog.cpp中
#include "dialog.h"
#include
#include
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
colorBtn = new QPushButton;
//控件初始化
colorBtn->setText(tr("颜色标准对话框"));
colorFrame = new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
//布局管理
mainLayout = new QGridLayout(this);
mainLayout->addWidget(colorBtn, 1, 0);
mainLayout->addWidget(colorFrame, 1, 1);
//
connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
}
Dialog::~Dialog()
{
}
void Dialog::showColor()
{
QColor c = QColorDialog::getColor(Qt::blue);
if(c.isValid()){
colorFrame->setPalette(QPalette(c));
}
}
4、效果
5、
QColor color = QColorDialog::getColor
(const QColor&initial = Qt::white,
QWidget * parent = 0);
第一个参数:对话框打开时默认选择的颜色(QColor类型)
第二个参数:它的parent