加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

深入理解Qt中各种消息框对话框的使用

发布时间:2020-12-30 19:20:42 所属栏目:创业 来源:网络整理
导读:最近在进修Qt框架,本日进修了一下动静框的行使, 现清算出来以作记录。 在措施运行时,常常必要提醒用户一些信息,好比告诫啊,提醒啊,提议啊之类的对象。这些对象根基上是通过动静框与用户举办交互的,Qt中首要是用QMessageBox类来加以实现的。 动静框一
副问题[/!--empirenews.page--]

最近在进修Qt框架,本日进修了一下动静框的行使, 现清算出来以作记录。

在措施运行时,常常必要提醒用户一些信息,好比告诫啊,提醒啊,提议啊之类的对象。这些对象根基上是通过动静框与用户举办交互的,Qt中首要是用QMessageBox类来加以实现的。

动静框一样平常分为七种:

  1. Question扣问动静框:为正常的操纵提供一个简朴的扣问
  2. Information信息动静框:为正常操纵提供一个提醒
  3. Warning提醒动静框:提示用户产生了一个错误
  4. Critical告诫动静框:告诫用户产生了一个严峻错误
  5. About关于动静框:自界说的关于信息
  6. AboutQt关于Qt动静框:Qt自身的关于信息
  7. Custom自界说动静框:本身定制动静框

详细用法见源码以及说明:

Dialog.pro

#-------------------------------------------------
#
# Project created by QtCreator 2015-10-24T17:32:35
#
#-------------------------------------------------

QT    += core gui

greaterThan(QT_MAJOR_VERSION,4): QT += widgets

TARGET = Dialog
TEMPLATE = app

SOURCES += main.cpp
    dialog.cpp

HEADERS += dialog.h

dialog.h

#ifndefDIALOG_H
#defineDIALOG_H

#include<QDialog>
#include<QGridLayout>
#include<QPushButton>
#include<QLabel>
#include<QMessageBox>
class Dialog: public QDialog
{
  Q_OBJECT

public:
  Dialog(QWidget *parent = 0);
  ~Dialog();
public://设置部件和机关
  QLabel *label;
  QPushButton *QuestionBtn,*InformationBtn,*WarningBtn,*CriticalBtn,*AboutBtn,*AboutQtBtn,*CustomBtn;
  QGridLayout *layout,*layoutLabel,*layoutBtn;
protected slots://各类按钮的槽
  void slotQuestion();
  void slotInformation();
  void slotWarning();
  void slotCritical();
  void slotAbout();
  void slotAboutQt();
  void slotCustom();
};

#endif// DIALOG_H

dialog.cpp

#include"dialog.h"

Dialog::Dialog(QWidget *parent)
  : QDialog(parent)
{
  setWindowTitle("QMessageBox");

  QuestionBtn=new QPushButton("Question");
  InformationBtn=new QPushButton("Information");
  WarningBtn=new QPushButton("Warning");
  CriticalBtn=new QPushButton("Critical");
  AboutBtn=new QPushButton("About");
  AboutQtBtn=new QPushButton("AboutQt");
  CustomBtn=new QPushButton("Custom");

  label=new QLabel("About Qt MessageBox:");
  layout=new QGridLayout(this);
  layoutLabel=new QGridLayout;
  layoutBtn=new QGridLayout;
  layoutLabel->addWidget(label,0);
  layoutBtn->addWidget(QuestionBtn,1,0);
  layoutBtn->addWidget(InformationBtn,1);
  layoutBtn->addWidget(WarningBtn,2,0);
  layoutBtn->addWidget(CriticalBtn,1);
  layoutBtn->addWidget(AboutBtn,3,0);
  layoutBtn->addWidget(AboutQtBtn,1);
  layoutBtn->addWidget(CustomBtn,4,0);
  layoutBtn->setSpacing(15);

  //嵌套机关
  layout->addLayout(layoutLabel,0);
  layout->addLayout(layoutBtn,0);
  setFixedSize(300,220);//牢靠巨细

  connect(QuestionBtn,SIGNAL(clicked()),this,SLOT(slotQuestion()));
  connect(InformationBtn,SLOT(slotInformation()));
  connect(WarningBtn,SLOT(slotWarning()));
  connect(CriticalBtn,SLOT(slotCritical()));
  connect(AboutBtn,SLOT(slotAbout()));
  connect(AboutQtBtn,SLOT(slotAboutQt()));
  connect(CustomBtn,SLOT(slotCustom()));
}

Dialog::~Dialog()
{

}

//直接挪用AboutQt,配置句柄和问题即可
void Dialog::slotAboutQt(){
 QMessageBox::aboutQt(this,"This is the title");
}

//以下三个函数均是配置句柄问题和信息即可,也可以在最后配置默认按钮,一样平常默认的是QMessageBox::Ok。
void Dialog::slotAbout(){
   QMessageBox::about(this,"About","This is the label.");
}
void Dialog::slotCritical(){
  QMessageBox::critical(this,"Critical","This is the label.");
}
void Dialog::slotInformation(){
 QMessageBox::information(this,"Information","This is the label.");
}

//自界说动静框
void Dialog::slotCustom(){

  QMessageBox customMsgBox;
  customMsgBox.setWindowTitle("Custom message box");

  //添加按键
  QPushButton *lockBtn=customMsgBox.addButton("Lock",QMessageBox::ActionRole);
  QPushButton *unlockBtn=customMsgBox.addButton("Unlock",QMessageBox::ActionRole);
  QPushButton *cancelBtn=customMsgBox.addButton(QMessageBox::Cancel);//留意cancel不能指定Text

  //customMsgBox.setIconPixmap(QPixmap("a.png"));//配置图片
  customMsgBox.setText("This is the label");
  customMsgBox.exec();//执动作静框

  QPushButton *msg=(QPushButton*)customMsgBox.clickedButton();//接管按键信息

  //判定按键
  if(msg==lockBtn)
    label->setText("Custom button /lock");

  if(msg==unlockBtn)
    label->setText("Custom button /unlock");

  if(msg==cancelBtn)
    label->setText("Custom button /cancel");

}

void Dialog::slotQuestion(){
  //QMessageBox::**question()**函数,传入句柄,问题,文本,按钮值,返回按键对应的值,最后也可以加默认按键的位置
  int msg=QMessageBox::question(this,"Question","This is the label.",QMessageBox::Ok|QMessageBox::Cancel);

  //判定选择信息
  switch(msg){
  case QMessageBox::Ok:
    label->setText("Question button /OK");
    break;
  case QMessageBox::Cancel:
    label->setText("Question button /Cancel");
    break;
  default:
    break;
  }
}

void Dialog::slotWarning(){

  //QmessageBox::warning()函数同Question函数
  int msg=QMessageBox::warning(this,QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save);

  switch(msg){//判定选择信息
  case QMessageBox::Save:
    label->setText("Warning button /Save");
    break;
  case QMessageBox::Cancel:
    label->setText("Warning button /Cancel");
    break;
  case QMessageBox::Discard:
    label->setText("Warning button /Discard");
    break;
  default:
    break;
  }

}

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读