QColorDialog经常使用的成员函数是getColor()函数,这个函数返回用户选择的颜色值。
QColor getColor
(
const QColor & initial=Qt::white,//指定默认选中的颜色,可以通过QColor::isValid()函数判断用户选择的颜色是否有效
QWidget *parent=0
);
//dialog.h
QPushButton *colorBtn;
QFrame *colorFrame;
void showColor();
//dialog.cpp
colorBtn=new QPushButton;
colorBtn->setText(tr("标准颜色对话框"));
colorFrame=new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
connect(colorBtn,SIGNAL(clicked()),this,SLOT(showColor()));
void Dialog::showColor()
{
QColor c=QColorDialog::getColor(Qt::blue);
if(c.isValid())
{
colorFrame->setPalette(QPalette(c));
}
}