// 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();
}
|