《qt 设置对话框中按钮》由会员分享,可在线阅读,更多相关《qt 设置对话框中按钮(4页珍藏版)》请在金锄头文库上搜索。
1、Qt 对话框及其按钮本文应该说主要是讲述 Qt 按钮的一些设置方法,主要内容为:标准按钮的创建、自设按钮、按钮文本的中英文显示、按钮的禁用、设置按钮大小、按钮的透明设置、默认按钮、按钮上图标加载、对话框上显示中英文文本、对话框标题等。程序中已给出解释:首先:.h 文件#ifndef MESSAGEBOX_H#define MESSAGEBOX_H#include class QLabel;class MessageBox : public QDialogQ_OBJECTpublic:MessageBox(QWidget *parent=0);public slots:void slotQues
2、tion();void slotInformation();void slotWarning();void slotCritical();void slotAbout();void slotAboutQt();void slotCustom();private:QLabel *label;#endif / MESSAGEBOX_H其次:.cpp 文件#include messagebox.h#include MessageBox:MessageBox(QWidget *parent): QDialog(parent)setWindowTitle(trUtf8(34525727135025723
3、5346241206345255246344271240347244272344276213);label = new QLabel; QPushButton *btn1 = new QPushButton(this);btn1-setText(trUtf8(347226221351227256); /显示按钮中文名称QPushButton *btn2 = new QPushButton(this);btn2-setText(trUtf8(344277241346201257); QPushButton *btn3 = new QPushButton(this);btn3-setText(tr
4、Utf8(346217220351206222); QPushButton *btn4 = new QPushButton(Critical);/按钮名称为英文QPushButton *btn5 = new QPushButton(About);QPushButton *btn6 = new QPushButton(About Qt);QPushButton *btn7 = new QPushButton(Custom);QGridLayout *grid = new QGridLayout;grid-addWidget(btn1,0,0);grid-addWidget(btn2,0,1);g
5、rid-addWidget(btn3,1,0);grid-addWidget(btn4,1,1);grid-addWidget(btn5,2,0);grid-addWidget(btn6,2,1);grid-addWidget(btn7,3,0);QVBoxLayout *mainLayout = new QVBoxLayout;mainLayout-setMargin(10);mainLayout-setSpacing(20);mainLayout-addWidget(label);mainLayout-addLayout(grid);setLayout(mainLayout);connec
6、t(btn1,SIGNAL(clicked(),this,SLOT(slotQuestion();connect(btn2,SIGNAL(clicked(),this,SLOT(slotInformation();connect(btn3,SIGNAL(clicked(),this,SLOT(slotWarning();connect(btn4,SIGNAL(clicked(),this,SLOT(slotCritical();connect(btn5,SIGNAL(clicked(),this,SLOT(slotAbout();connect(btn6,SIGNAL(clicked(),th
7、is,SLOT(slotAboutQt();connect(btn7,SIGNAL(clicked(),this,SLOT(slotCustom();void MessageBox:slotCustom()QMessageBox customMsgBox;customMsgBox.setWindowTitle(Custom message box);QPushButton *unlockButton = customMsgBox.addButton(tr(Unlock),QMessageBox:ActionRole);/自设按钮,默认焦点为第一个QPushButton *lockButton
8、= customMsgBox.addButton(tr(Lock),QMessageBox:ActionRole); QPushButton *cancelButton = customMsgBox.addButton(QMessageBox:Cancel);/使用标准按钮QMessageBox:StandardButtoncustomMsgBox.setIconPixmap(QPixmap(D:/warning.png); /加载对话框中的图标,图标显示的大小按实际大小显示,所以应该注意图标的大小customMsgBox.setText(tr(This is a custom message
9、 box);/设置对话框的标题cancelButton-setDefault(true);/设置”Cancel“按钮为默认按钮 不起作用?/lockButton-setEnabled(false); /设置”Lock“按钮为禁用按钮(禁用)lockButton-setFlat(true); /把”Lock“按钮设置为透明按钮QPixmap icon;icon = style()-standardPixmap(QStyle:SP_DriveDVDIcon);/按钮上加载系统图片,参见enum QStyle:StandardPixmapcancelButton-setIcon(icon);lock
10、Button-setIcon(QIcon(D:/Winter.jpg);/加载自定义图片lockButton-setIconSize(QSize(24, 24);/设置按钮的大小customMsgBox.exec();if(customMsgBox.clickedButton() = lockButton)label-setText( Custom MessageBox / Lock );if(customMsgBox.clickedButton() = unlockButton)label-setText( Custom MessageBox / Unlock );if(customMsgB
11、ox.clickedButton() = cancelButton)label-setText( Custom MessageBox / Cancel );return;void MessageBox:slotWarning()switch(QMessageBox:warning(this,Warning,tr(Save changes to document?),QMessageBox:Save|QMessageBox:Discard|QMessageBox:Cancel,QMessageBox:Cancel)/参数为父窗口指针、参数为对话框标题、参数为对话框中显示的文本、/参数为设置对话框
12、中的按钮(全部为标准按钮)、最后一个参数为设置默认按钮case QMessageBox:Save:label-setText( Warning button / Save );break;case QMessageBox:Discard:label-setText( Warning button / Discard );break; case QMessageBox:Cancel:label-setText( Warning button / Cancel );break;default:break;return; void MessageBox:slotQuestion() void MessageBox:slotInformation() void MessageBox:slotCritical() void MessageBox:slotAbout() void MessageBox:slotAboutQt() 后面的函数代码可以参照前面的,在此就不介绍。