操作系统:Fedora Linux 14
创建文件夹InputDialog,以下是代码(三个文件)
/** Object: MessageBox
** Desc: 各种信息框的使用
** File: main.cpp
** Compile:qmake-qt4 -project;qmake-qt4;make;
** Author: LiXiujie
** Date: 2011-05-12
** Note: 编译说明:
** qmake-qt4 -prject 自动生成程序的项目文件(*.pro);
** qmake-qt4 用于生成程序的Makefile文件;
** make 编译 Makefile 文件得到可执行文件。
** */
#include <QApplication> // 所有QT图形化应用程序必须包含此文件,它包含了QT图形化应用程序的各种资源、基本设置、控制流及事件处理等。
#include "MessageBox.h" // 自定义类头文件
int main(int argc, char *argv[]){
QApplication app(argc, argv);
MessageBox *mb = new MessageBox();
mb->show();
return app.exec();
}
==========================
/** Object: MessageBox
** Desc: 各种信息框的使用
** File: MessageBox.h
** Class: MessageBox各种信息框的使用类 头文件
** Compile:qmake-qt4 -project;qmake-qt4;make;
** Author: LiXiujie
** Date: 2011-05-12
** Note: 编译说明:
** qmake-qt4 -prject 自动生成程序的项目文件(*.pro);
** qmake-qt4 用于生成程序的Makefile文件;
** make 编译 Makefile 文件得到可执行文件。
** */
#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H
#include <QtGui> // 包含了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
===================================
/** Object: MessageBox
** Desc: 各种信息框的使用
** File: MessageBox.h
** Class: MessageBox各种信息框的使用类 源文件
** Compile:qmake-qt4 -project;qmake-qt4;make;
** Author: LiXiujie
** Date: 2011-05-12
** Note: 编译说明:
** qmake-qt4 -prject 自动生成程序的项目文件(*.pro);
** qmake-qt4 用于生成程序的Makefile文件;
** make 编译 Makefile 文件得到可执行文件。
** */
#include "MessageBox.h"
MessageBox::MessageBox(QWidget *parent): QDialog(parent){
setWindowTitle(tr("Message Box Example"));
m_pLabel = new QLabel; // 标签控件实例化
QPushButton *pPB1 = new QPushButton("Question"); // 按钮控件实例化
QPushButton *pPB2 = new QPushButton("Information");
QPushButton *pPB3 = new QPushButton("Warning");
QPushButton *pPB4 = new QPushButton("Critical");
QPushButton *pPB5 = new QPushButton("About");
QPushButton *pPB6 = new QPushButton("About Qt");
QPushButton *pPB7 = new QPushButton("Custom");
QGridLayout *pGL = new QGridLayout; // 表格布局控件
pGL->addWidget(pPB1,0,0);
pGL->addWidget(pPB2,0,1);
pGL->addWidget(pPB3,1,0);
pGL->addWidget(pPB4,1,1);
pGL->addWidget(pPB5,2,0);
pGL->addWidget(pPB6,2,1);
pGL->addWidget(pPB7,3,0);
QVBoxLayout *pVBL = new QVBoxLayout; // 垂直布局控件
pVBL->setMargin(10);
pVBL->setSpacing(20);
pVBL->addWidget(m_pLabel);
pVBL->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, "Question", tr("It's end of document,search from begin?"),
QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Ok)) // Yes/No询问信息对话框
{
case QMessageBox::Ok:
m_pLabel->setText(" Question button / Ok ");
break;
case QMessageBox::Cancel:
m_pLabel->setText(" Question button / Cancel ");
break;
default:
break;
}
return;
}
void MessageBox::slotInformation(){
QMessageBox::information(this, "Information", tr("anything you want tell user")); // 一般信息提示对话框
return;
}
void MessageBox::slotWarning(){
switch(QMessageBox::warning(this, "Warning", tr("Save changes to document?"),
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel, QMessageBox::Save)) // 警告信息对话框
{
case QMessageBox::Save:
m_pLabel->setText(" Warning button / Save ");
break;
case QMessageBox::Discard:
m_pLabel->setText(" Warning button / Discard ");
break;
case QMessageBox::Cancel:
m_pLabel->setText(" Warning button / Cancel ");
break;
default:
break;
}
return;
}
void MessageBox::slotCritical(){
QMessageBox::critical(this, "Critical", tr("tell user a critical error")); // 严重错误信息对话框
m_pLabel->setText(" Critical MessageBox ");
return;
}
void MessageBox::slotAbout()
{
QMessageBox::about(this, "About", tr("Message box example!")); // 关于我对话框
m_pLabel->setText(" About MessageBox ");
return;
}
void MessageBox::slotAboutQt()
{
QMessageBox::aboutQt(this,"About Qt"); // 关于QT信息对话框
m_pLabel->setText(" About Qt MessageBox ");
return;
}
void MessageBox::slotCustom()
{
QMessageBox customMsgBox;
customMsgBox.setWindowTitle("Custom message box"); // 设置信息对话框标题
QPushButton *lockButton = customMsgBox.addButton(tr("Lock"), QMessageBox::ActionRole);
QPushButton *unlockButton = customMsgBox.addButton(tr("Unlock"), QMessageBox::ActionRole);
QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);
customMsgBox.setIconPixmap(QPixmap("images/fedora.jpg"));
customMsgBox.setText(tr("This is a custom message box"));
customMsgBox.exec();
if(customMsgBox.clickedButton() == lockButton)
m_pLabel->setText(" Custom MessageBox / Lock ");
if(customMsgBox.clickedButton() == unlockButton)
m_pLabel->setText(" Custom MessageBox / Unlock ");
if(customMsgBox.clickedButton() == cancelButton)
m_pLabel->setText(" Custom MessageBox / Cancel ");
return;
}
图: