QT学习 第一章:基本对话框--各种信息框的使用

操作系统:Fedora Linux 14
创建文件夹MessageBox,以下是代码(三个文件)

<textarea cols="50" rows="15" name="code" class="cpp">/** Object: MessageBox ** Desc: 各种信息框的使用 ** File: main.cpp ** Compile:qmake-qt4 -project;qmake-qt4;make; ** Author: LiXiujie www.xiujie.cn ** Date: 2011-05-12 ** Note: 编译说明: ** qmake-qt4 -prject 自动生成程序的项目文件(*.pro); ** qmake-qt4 用于生成程序的Makefile文件; ** make 编译 Makefile 文件得到可执行文件。 ** */ #include &lt;QApplication&gt; // 所有QT图形化应用程序必须包含此文件,它包含了QT图形化应用程序的各种资源、基本设置、控制流及事件处理等。 #include &quot;MessageBox.h&quot; // 自定义类头文件 int main(int argc, char *argv[]){ QApplication app(argc, argv); MessageBox *mb = new MessageBox(); mb-&gt;show(); return app.exec(); } </textarea>

 

<textarea cols="50" rows="15" name="code" class="cpp">/** Object: MessageBox ** Desc: 各种信息框的使用 ** File: MessageBox.h ** Class: MessageBox各种信息框的使用类 头文件 ** Compile:qmake-qt4 -project;qmake-qt4;make; ** Author: LiXiujie www.xiujie.cn ** Date: 2011-05-12 ** Note: 编译说明: ** qmake-qt4 -prject 自动生成程序的项目文件(*.pro); ** qmake-qt4 用于生成程序的Makefile文件; ** make 编译 Makefile 文件得到可执行文件。 ** */ #ifndef MESSAGEBOX_H #define MESSAGEBOX_H #include &lt;QtGui&gt; // 包含了QT基本头文件和GUI头文件。GUI:图形用户界面。 class MessageBox : public QDialog { Q_OBJECT public: MessageBox(QWidget *parent=0); private: QLabel *m_pLabel; // 标签控件 private slots: // 槽 void slotQuestion(); void slotInformation(); void slotWarning(); void slotCritical(); void slotAbout(); void slotAboutQt(); void slotCustom(); }; #endif // MESSAGEBOX_H </textarea>

 

<textarea cols="50" rows="15" name="code" class="cpp">/** Object: MessageBox ** Desc: 各种信息框的使用 ** File: MessageBox.h ** Class: MessageBox各种信息框的使用类 源文件 ** Compile:qmake-qt4 -project;qmake-qt4;make; ** Author: LiXiujie www.xiujie.cn ** Date: 2011-05-12 ** Note: 编译说明: ** qmake-qt4 -prject 自动生成程序的项目文件(*.pro); ** qmake-qt4 用于生成程序的Makefile文件; ** make 编译 Makefile 文件得到可执行文件。 ** */ #include &quot;MessageBox.h&quot; MessageBox::MessageBox(QWidget *parent): QDialog(parent){ setWindowTitle(tr(&quot;Message Box Example&quot;)); m_pLabel = new QLabel; // 标签控件实例化 QPushButton *pPB1 = new QPushButton(&quot;Question&quot;); // 按钮控件实例化 QPushButton *pPB2 = new QPushButton(&quot;Information&quot;); QPushButton *pPB3 = new QPushButton(&quot;Warning&quot;); QPushButton *pPB4 = new QPushButton(&quot;Critical&quot;); QPushButton *pPB5 = new QPushButton(&quot;About&quot;); QPushButton *pPB6 = new QPushButton(&quot;About Qt&quot;); QPushButton *pPB7 = new QPushButton(&quot;Custom&quot;); QGridLayout *pGL = new QGridLayout; // 表格布局控件 pGL-&gt;addWidget(pPB1,0,0); pGL-&gt;addWidget(pPB2,0,1); pGL-&gt;addWidget(pPB3,1,0); pGL-&gt;addWidget(pPB4,1,1); pGL-&gt;addWidget(pPB5,2,0); pGL-&gt;addWidget(pPB6,2,1); pGL-&gt;addWidget(pPB7,3,0); QVBoxLayout *pVBL = new QVBoxLayout; // 垂直布局控件 pVBL-&gt;setMargin(10); pVBL-&gt;setSpacing(20); pVBL-&gt;addWidget(m_pLabel); pVBL-&gt;addLayout(pGL); setLayout(pVBL); // 本对话框使用垂直布局控件 /* 绑定按钮单击事件处理函数 */ connect(pPB1, SIGNAL(clicked()), this, SLOT(slotQuestion())); connect(pPB2, SIGNAL(clicked()), this, SLOT(slotInformation())); connect(pPB3, SIGNAL(clicked()), this, SLOT(slotWarning())); connect(pPB4, SIGNAL(clicked()), this, SLOT(slotCritical())); connect(pPB5, SIGNAL(clicked()), this, SLOT(slotAbout())); connect(pPB6, SIGNAL(clicked()), this, SLOT(slotAboutQt())); connect(pPB7, SIGNAL(clicked()), this, SLOT(slotCustom())); } void MessageBox::slotQuestion(){ switch(QMessageBox::question(this, &quot;Question&quot;, tr(&quot;It's end of document,search from begin?&quot;), QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Ok)) // Yes/No询问信息对话框 { case QMessageBox::Ok: m_pLabel-&gt;setText(&quot; Question button / Ok &quot;); break; case QMessageBox::Cancel: m_pLabel-&gt;setText(&quot; Question button / Cancel &quot;); break; default: break; } return; } void MessageBox::slotInformation(){ QMessageBox::information(this, &quot;Information&quot;, tr(&quot;anything you want tell user&quot;)); // 一般信息提示对话框 return; } void MessageBox::slotWarning(){ switch(QMessageBox::warning(this, &quot;Warning&quot;, tr(&quot;Save changes to document?&quot;), QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel, QMessageBox::Save)) // 警告信息对话框 { case QMessageBox::Save: m_pLabel-&gt;setText(&quot; Warning button / Save &quot;); break; case QMessageBox::Discard: m_pLabel-&gt;setText(&quot; Warning button / Discard &quot;); break; case QMessageBox::Cancel: m_pLabel-&gt;setText(&quot; Warning button / Cancel &quot;); break; default: break; } return; } void MessageBox::slotCritical(){ QMessageBox::critical(this, &quot;Critical&quot;, tr(&quot;tell user a critical error&quot;)); // 严重错误信息对话框 m_pLabel-&gt;setText(&quot; Critical MessageBox &quot;); return; } void MessageBox::slotAbout() { QMessageBox::about(this, &quot;About&quot;, tr(&quot;Message box example!&quot;)); // 关于我对话框 m_pLabel-&gt;setText(&quot; About MessageBox &quot;); return; } void MessageBox::slotAboutQt() { QMessageBox::aboutQt(this,&quot;About Qt&quot;); // 关于QT信息对话框 m_pLabel-&gt;setText(&quot; About Qt MessageBox &quot;); return; } void MessageBox::slotCustom() { QMessageBox customMsgBox; customMsgBox.setWindowTitle(&quot;Custom message box&quot;); // 设置信息对话框标题 QPushButton *lockButton = customMsgBox.addButton(tr(&quot;Lock&quot;), QMessageBox::ActionRole); QPushButton *unlockButton = customMsgBox.addButton(tr(&quot;Unlock&quot;), QMessageBox::ActionRole); QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel); customMsgBox.setIconPixmap(QPixmap(&quot;images/fedora.jpg&quot;)); customMsgBox.setText(tr(&quot;This is a custom message box&quot;)); customMsgBox.exec(); if(customMsgBox.clickedButton() == lockButton) m_pLabel-&gt;setText(&quot; Custom MessageBox / Lock &quot;); if(customMsgBox.clickedButton() == unlockButton) m_pLabel-&gt;setText(&quot; Custom MessageBox / Unlock &quot;); if(customMsgBox.clickedButton() == cancelButton) m_pLabel-&gt;setText(&quot; Custom MessageBox / Cancel &quot;); return; } </textarea>

图:

你可能感兴趣的:(QT学习 第一章:基本对话框--各种信息框的使用)