Qt5基本对话框学习

来源:互联网 发布:淘宝物流宝平台 编辑:程序博客网 时间:2024/05/18 13:43

书籍:《Qt5开发及实例》(第二版) 陆文周

1.标准颜色对话框类

colorBtn=new QPushButton;                                   //创建各个控件的对象colorBtn->setText(QString::fromLocal8Bit ("颜色标准对话框实例"));colorFrame=new QFrame;colorFrame->setFrameShape(QFrame::Box);colorFrame->setAutoFillBackground(true);mainLayout->addWidget(colorBtn,1,0);                        //布局设计mainLayout->addWidget(colorFrame,1,1);connect(colorBtn,SIGNAL(clicked()),this,SLOT(showColor())); //事件关联

当setAutoFillBackground为false时, 不能正常取色

QFrame类可参考http://www.kuqin.com/qtdocument/qframe.html#details

槽函数感觉c一直都是valid,去掉if也没啥问题

void Dialog::showColor(){    QColor c = QColorDialog::getColor(Qt::blue);    if(c.isValid())    {        colorFrame->setPalette(QPalette(c));    }}

2.标准字体对话框类

void Dialog::showFont(){    bool ok;    QFont f = QFontDialog::getFont(&ok);    if (ok)    {        fontLineEdit->setFont(f);    }}

设置字体的槽函数,之前一直有些转不过来为什么要bool ok;
发现书上就有解释,之前一直没仔细看:
若用户选择“OK”,则该参数*ok将设为true,函数返回用户所选择的字体;
否则,将设为false,函数返回默认字体

好像挺简单的一个点,之前怎么不能自己理解..平时bool用的太少了

3.自定义消息框

void Dialog::showCustomDlg(){    label->setText(tr("Custom Message Box"));    QMessageBox customMsgBox;    customMsgBox.setWindowTitle(QString::fromLocal8Bit ("用户自定义消息框"));   //设置消息框的标题    QPushButton *yesBtn=customMsgBox.addButton(tr("Yes"),QMessageBox::ActionRole);    QPushButton *noBtn=customMsgBox.addButton(tr("No"),QMessageBox::ActionRole);    QPushButton *cancelBtn=customMsgBox.addButton(QMessageBox::Cancel);    customMsgBox.setText(QString::fromLocal8Bit ("这是一个用户自定义消息框!"));    customMsgBox.setIconPixmap(QPixmap("Qt.png"));    customMsgBox.exec();    if(customMsgBox.clickedButton()==yesBtn)        label->setText("Custom Message Box/Yes");    if(customMsgBox.clickedButton()==noBtn)        label->setText("Custom Message Box/No");    if(customMsgBox.clickedButton()==cancelBtn)        label->setText("Custom Message Box/Cancel");    return;}

QMessageBox为一个可以直接用的标准对话框

customMsgBox.setIconPixmap(QPixmap(“Qt.png”));
是一个插入图片的操作,应将图片放入build后的debug目录下