Qt でのさまざまなメッセージ ボックス ダイアログ ボックスの使用

プログラムの実行中、多くの場合、警告、ヒント、提案などの情報の入力をユーザーに求める必要があります。これらは基本的にメッセージ ボックスを通じてユーザーと対話します。メッセージ ボックスは主に Qt の QMessageBox クラスによって実現されます。

メッセージ ボックスは通常、次の 7 種類に分類されます。

  1. 質問クエリ メッセージ ボックス: 通常の操作のための簡単なクエリを提供します。
  2. 情報メッセージ ボックス: 通常の操作に関するヒントを提供します。
  3. 警告プロンプト メッセージ ボックス: エラーが発生したことをユーザーに通知します
  4. 重大な警告メッセージ ボックス: 重大なエラーが発生したことをユーザーに警告します。
  5. メッセージボックスについて: カスタム情報
  6. AboutQt Qt メッセージ ボックス: Qt 自体に関する情報
  7. カスタム カスタム メッセージ ボックス: メッセージ ボックスを自分でカスタマイズします。

具体的な使用方法については、ソース コードと分析を参照してください。

ダイアログプロ

#-------------------------------------------------
#
# 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

ダイアログ.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

ダイアログ.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,0);
  layoutBtn->addWidget(QuestionBtn,1,0);
  layoutBtn->addWidget(InformationBtn,1,1);
  layoutBtn->addWidget(WarningBtn,2,0);
  layoutBtn->addWidget(CriticalBtn,2,1);
  layoutBtn->addWidget(AboutBtn,3,0);
  layoutBtn->addWidget(AboutQtBtn,3,1);
  layoutBtn->addWidget(CustomBtn,4,0);
  layoutBtn->setSpacing(15);

  //嵌套布局
  layout->addLayout(layoutLabel,0,0);
  layout->addLayout(layoutBtn,1,0);
  setFixedSize(300,220);//固定大小

  connect(QuestionBtn,SIGNAL(clicked()),this,SLOT(slotQuestion()));
  connect(InformationBtn,SIGNAL(clicked()),this,SLOT(slotInformation()));
  connect(WarningBtn,SIGNAL(clicked()),this,SLOT(slotWarning()));
  connect(CriticalBtn,SIGNAL(clicked()),this,SLOT(slotCritical()));
  connect(AboutBtn,SIGNAL(clicked()),this,SLOT(slotAbout()));
  connect(AboutQtBtn,SIGNAL(clicked()),this,SLOT(slotAboutQt()));
  connect(CustomBtn,SIGNAL(clicked()),this,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,"Question","This is the label.",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;
  }

}

##メイン.cpp

#include"dialog.h"
#include<QApplication>

int main(intargc,char*argv[])
{
  QApplicationa(argc, argv);
  Dialog w;
  w.show();

  return a.exec();
}

スクリーンショットを実行する

この記事の利点は、 Qt 開発学習教材パッケージ、技術ビデオ (C++ 言語基礎、C++ デザイン パターン、Q​​t プログラミング入門、QT シグナルとスロット メカニズム、QT インターフェイス開発イメージ描画、QT ネットワーク、QT を含む) を無料で受け取ることです。データベースプログラミング、QTプロジェクト実戦、QSS、OpenCV、Quickモジュール、面接での質問など) ↓↓↓↓下記参照↓↓料金受け取りは記事下部をクリック↓↓ 

おすすめ

転載: blog.csdn.net/m0_73443478/article/details/130705414