1. 程式人生 > >Qt中的標準對話框之QMessageBox

Qt中的標準對話框之QMessageBox

cas data- 方式 stand tex 按鍵 對象 setprop 獲取

1. Qt標準對話框

  • Qt為開發者提供了一些可復用的對話框類型
  • Qt提供的可復用對話框全部繼承自QDialog類
  • Qt中的對話框的使用方式和QDialog完全一致

技術分享

2. 標準對話框的使用步驟

①定義對話框

DialogType dlg(this) ;

②設置對話框屬性

dlg.setPropertyXXX(value);

③打開對話框(判斷對話框返回值)

dlg.exec()==DialogType::value

④獲取對話框數據

Type v = dlg.getDialogValue();

⑤處理對話框數據

3.消息對話框

用於:為用戶提示重要信息,強制用戶進行選擇

消息對話框中的實用靜態函數

  ①QMessageBox::question(…) //創建帶有“是/否”按鈕的消息框

  ②QMessageBox::information(…) //創建帶有“確定”或“取消”按鈕的消息框

  ③QMessageBox::warning(…) //創建一個警告對話框

  ④QMessageBox::critical(…) //創建一個嚴重錯誤對話框

  ⑤QMessageBox::about(…) //創建“關於”對話框

4.實例

①一般步驟:

    
//1.構造消息對話框對象
QMessageBox msg(this);
//2.設置消息對話框屬性 msg.setWindowTitle("Window Title"); msg.setText("This is a detail message dialog!"); msg.setIcon(QMessageBox::Information); msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);
//3. 判斷消息對話框的返回值並處理 if( msg.exec() == QMessageBox::Ok ) { qDebug() << "Ok button is clicked!"; }

  

②實例演示

新建一個QWidget程序

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT
private:
    QPushButton question_MsgBtn;
    QPushButton infomation_MsgBtn;
    QPushButton warning_MsgBtn;
    QPushButton critical_MsgBtn;
    QPushButton about_MsgBtn;

private slots://定義為私有的槽函數,只有在類的內部可以訪問
    void question_Clicked();   //小技巧:單擊函數名,同時按下Alt+Enter鍵  選擇在widght中更添加定義可以直接編寫槽函數
    void infomation_Clicked();
    void warning_Clicked();
    void critical_Clicked();
    void about_Clicked();
public:
    Widget(QWidget *parent = 0);
    ~Widget();
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QMessageBox>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent),question_MsgBtn(this),infomation_MsgBtn(this),
      warning_MsgBtn(this),critical_MsgBtn(this),about_MsgBtn(this)
{
    question_MsgBtn.setText("question_MsgBtn Message Dialog");
    question_MsgBtn.move(20, 20);
    question_MsgBtn.resize(210, 30);

    infomation_MsgBtn.setText("infomation_MsgBtn Message Dialog");
    infomation_MsgBtn.move(20, 70);
    infomation_MsgBtn.resize(210, 30);

    warning_MsgBtn.setText("warning_MsgBtn Message Dialog");
    warning_MsgBtn.move(20, 120);
    warning_MsgBtn.resize(210, 30);

    critical_MsgBtn.setText("critical_MsgBtn Message Dialog");
    critical_MsgBtn.move(20, 170);
    critical_MsgBtn.resize(210, 30);

    about_MsgBtn.setText("about_MsgBtn Message Dialog");
    about_MsgBtn.move(20,230);
    about_MsgBtn.resize(210,30);

    resize(250, 290);
    setFixedSize(250, 290);

    connect(&question_MsgBtn,SIGNAL(clicked()),this ,SLOT(question_Clicked()));
    connect(&infomation_MsgBtn,SIGNAL(clicked()),this ,SLOT(infomation_Clicked()));
    connect(&warning_MsgBtn,SIGNAL(clicked()),this ,SLOT(warning_Clicked()));
    connect(&critical_MsgBtn,SIGNAL(clicked()),this ,SLOT(critical_Clicked()));
    connect(&about_MsgBtn,SIGNAL(clicked()),this ,SLOT(about_Clicked()));
}

Widget::~Widget()
{

}

void Widget::question_Clicked()
{
    QMessageBox msg(this);

    msg.setText("This is a detail message dialog!");
    msg.setIcon(QMessageBox::Question);
    msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);

    switch(msg.exec()){
    case (QMessageBox::Ok) :  //判斷哪個按鍵被按下
    {
      qDebug() << "Ok button is clicked!";
      break;
    }

    case (QMessageBox::Cancel) ://判斷哪個按鍵被按下
    {
      qDebug() << "Cancel button is clicked!";
      break;
    }

    case QMessageBox::YesToAll : //判斷哪個按鍵被按下
    {
      qDebug() << "YesToAll button is clicked!";
      break;
    }

   }
}

void Widget::infomation_Clicked()
{
    QMessageBox msg(this);

    msg.setText("This is  infomation  message dialog!");
    msg.setIcon(QMessageBox::Information);
    msg.setStandardButtons(QMessageBox::Help | QMessageBox::No | QMessageBox::Yes);

    switch(msg.exec()){
    case (QMessageBox::Help) :  //判斷哪個按鍵被按下
    {
      qDebug() << "Help button is clicked!";
      break;
    }

    case (QMessageBox::No) ://判斷哪個按鍵被按下
    {
      qDebug() << "No button is clicked!";
      break;
    }

    case QMessageBox::Yes : //判斷哪個按鍵被按下
    {
      qDebug() << "Yes button is clicked!";
      break;
    }

   }
}

void Widget::warning_Clicked()
{
//
}

void Widget::critical_Clicked()
{

}

void Widget::about_Clicked()
{

}

 

main.cpp

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

xiaoguotu:

技術分享

技術分享 技術分享

Qt中的標準對話框之QMessageBox