标签:com blog class div code c log t sp ext string
顾名思义,消息框的作用是给用户提供一些提醒或一些简单的询问。主要由QMessageBox类实现,qt提供的这个消息框类基本涵盖了开发应用中使用的各种情况,并且提供了自定义消息框的方式,满足各种特殊的需求,在实际应用中关键是分析实际的应用需求,根据不同的应用环境选择最合适的消息框,以使程序简洁而合理。
这一次,书中连类的实现都没有,只给了一些函数接口,以后会怎么样呢?画面太残暴不敢想。
学习过程中又有了一些小收获,如qt自带的帮助文档,非常全面,当鼠标放在某个qt类的名称时,按F1就会弹出此类的说明文档,十分便捷。
代码:
// messagebox.h #ifndef MESSAGEBOX_H #define MESSAGEBOX_H #include <QPushButton> #include <QLabel> #include <QGridLayout> #include <QMessageBox> #include <QDialog> class
messagebox: public
QDialog // 必须加上public QDialog,因为要从QDialog类中继承很多内容 { Q_OBJECT public : messagebox(); ~messagebox(); public : QLabel *label; QPushButton *quest; QPushButton *info; QPushButton *warn; QPushButton *crtcal; QPushButton *about; QPushButton *aboutQt; QPushButton *custom; QGridLayout *layout; private
slots: void
slotQuestion(); void
slotInformation(); void
slotWarning(); void
slotCritical(); void
slotAbout(); void
slotAboutQt(); void
slotCustom(); }; #endif // MESSAGEBOX_H // messagebox.cpp #include "messagebox.h" messagebox::messagebox() { // 创建元素 label = new
QLabel; label->setText(tr( "About Qt Message Box" )); quest = new
QPushButton; quest->setText(tr( "Question" )); info = new
QPushButton; info->setText(tr( "Information" )); warn = new
QPushButton; warn->setText(tr( "Warning" )); crtcal = new
QPushButton; crtcal->setText(tr( "Critical" )); about = new
QPushButton; about->setText(tr( "About" )); aboutQt = new
QPushButton; aboutQt->setText(tr( "About Qt" )); custom = new
QPushButton; custom->setText(tr( "custom" )); layout = new
QGridLayout( this ); // 最初没有加上this,显示只有一个空白的方框,下列元素没有显示在其中。 // 布局 layout->addWidget(label, 0, 0); layout->addWidget(quest, 0 , 1); layout->addWidget(info, 1, 1); layout->addWidget(warn, 1, 0); layout->addWidget(crtcal, 2, 1); layout->addWidget(about, 2, 0); layout->addWidget(aboutQt, 3, 1); layout->addWidget(custom, 3, 0); // 连接槽 connect(quest, SIGNAL(clicked()), this , SLOT(slotQuestion()) ); connect(about, SIGNAL(clicked()), this , SLOT(slotAbout())); connect(aboutQt, SIGNAL(clicked()), this , SLOT(slotAboutQt())); connect(info, SIGNAL(clicked()), this , SLOT(slotInformation())); connect(warn, SIGNAL(clicked()), this , SLOT(slotWarning())); connect(crtcal, SIGNAL(clicked()), this , SLOT(slotCritical())); connect(custom, SIGNAL(clicked()), this , SLOT(slotCustom())); } messagebox::~messagebox() { delete
label; delete
layout; delete
quest; delete
about; delete
aboutQt; delete
info; delete
warn; delete
crtcal; delete
custom; } void
messagebox::slotQuestion() { switch (QMessageBox::question( this , "Question" ,tr( "It‘s end of document, search from begin?" ), QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Ok)) { case
QMessageBox::Ok: label->setText( "Question button /Ok" ); break ; case
QMessageBox::Cancel: label->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: 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::slotCritical() { QMessageBox::critical( this , "Information" , tr( "tell user a critical error" )); label->setText( "Critical MessageBox" ); return ; } void
messagebox::slotAbout() { QMessageBox::about( this , "About" , tr( "Message box example" )); label->setText( "About MessageBox" ); return
; } void
messagebox::slotAboutQt() { QMessageBox::aboutQt( this , "About Qttttttt" ); // 设置消息框名称 label->setText( "About Qt MessageBox" ); return
; } void
messagebox::slotCustom() { QMessageBox customMsgBox; // 用鼠标点QMessageBOx, 然后按F1, 将出现QMessageBox的内部说明文档 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( "" )); customMsgBox.setText( "This is a custom message box" ); customMsgBox.exec(); if (customMsgBox.clickedButton() == lockButton) label->setText( "Custom MessageBox /Lock" ); if (customMsgBox.clickedButton() == unlockButton) label->setText( "Custom MessageBox/ Unlock" ); if (customMsgBox.clickedButton() == cancelButton) label->setText( "Custom MessageBox/Cancel" ); return
; } // main.cpp #include <QApplication> #include <QPushButton> #include <QLabel> #include "standarddialogs.h" #include "geometry.h" #include "inputdialog.h" #include "messagebox.h" int
main( int
argc, char
*argv[]) { QApplication a(argc, argv); // StandardDialogs ct; // ct.show(); // QPushButton b("Hello World!"); // b.show(); // QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit())); // Geometry my; // my.show(); // InputDlg person; // person.show(); messagebox msgb; msgb.show(); return
a.exec(); } |
【qt学习003】渐入佳境——各种标准消息框的使用,布布扣,bubuko.com
标签:com blog class div code c log t sp ext string
原文地址:http://www.cnblogs.com/xy-kidult/p/3701202.html