c 语言 信息框 linux,各种信息框的使用 - QT学习 第一章:基本对话框_Linux编程_Linux公社-Linux系统门户网站...

操作系统:Fedora Linux 14

创建文件夹InputDialog,以下是代码(三个文件)

/** Object: MessageBox

** Desc:   各种信息框的使用

** File:   main.cpp

** Compile:qmake-qt4 -project;qmake-qt4;make;

** Author: LiXiujie www.linuxidc.com

** Date:  2011-05-12

** Note:  编译说明:

**      qmake-qt4 -prject 自动生成程序的项目文件(*.pro);

**      qmake-qt4 用于生成程序的Makefile文件;

**      make 编译 Makefile 文件得到可执行文件。

** */

#include // 所有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 www.linuxidc.com

** Date:  2011-05-12

** Note:  编译说明:

**      qmake-qt4 -prject 自动生成程序的项目文件(*.pro);

**      qmake-qt4 用于生成程序的Makefile文件;

**      make 编译 Makefile 文件得到可执行文件。

** */

#ifndef MESSAGEBOX_H

#define MESSAGEBOX_H

#include // 包含了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 www.linuxidc.com

** 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;

}

图:

84b94ef5da193d394a905b131e29ed42.gif0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值