QMessageBox information modal dialog box detailed usage tutorial, object creation stack and pointer type, dialog box style setting, don't waste real time to encapsulate it yourself, with pictures and texts, just look at the pictures and talk.

There are two types of embedded and PC

interface design

All of the above buttons can be accessed directly by going to the slot
insert image description here

Display of results

The so-called picture has the truth, if you think it is what your project needs, just use it
insert image description here
I carefully designed one
insert image description here

[1] Use QMessageBox on the PC side

No need to design and repackage by yourself, just call
特色: The place where the text is displayed is actually the QLabel class, and the official set his picture and text in it.
The same is true for buttons, which are QPushButtons. This box will be dynamically adjusted according to your text content, if you want to fix the size, you can set it through the style sheet

information (general information)

[static] QMessageBox::StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

Intrinsic function implementation

void MainWindow::on_PB_mBox_clicked()
{
    
    
    QMessageBox::information(this,"infomation","信息");
}

insert image description here


warning (warning message)

[static] QMessageBox::StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
void MainWindow::on_PB_warning_clicked()
{
    
    
    QMessageBox::warning(this,"warning","信息");
}

insert image description here


critical (error message)

[static] QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
void MainWindow::on_PB_error_clicked()
{
    
    
    QMessageBox::critical(this,"critical","信息");
}

insert image description here


about (about information, no button)

Displays a simple about box with title title and text text.

[static] void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
void MainWindow::on_PB_abort_clicked()
{
    
    
     QMessageBox::about(this,"about","信息");	

     QMessageBox::aboutQt(this,"aboutQt");	//显示Qt的官方介绍
}

insert image description here
insert image description here


question (question information?)

Opens a question message box with the given title and text in front of the specified parent widget.

[static] QMessageBox::StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
void MainWindow::on_PB_quest_clicked()
{
    
    
    QMessageBox::question(this,"question","信息");
}

insert image description here


Embedded platforms use QMessageBox

On embedded platforms, resistive and capacitive screens are sometimes used as touch screens. At this time, the button text must be set larger, so you have to do it yourself
Here are two implementations using stacks and pointers.

The focus is on styling

QMessageBox (stack object)

I won't introduce them one by one in detail, just take a look according to your own needs.

void MainWindow::on_PB_struct_clicked()
{
    
    
    QMessageBox box;
    //去除标题和框
    box.setWindowFlag(Qt::FramelessWindowHint); //这种设置后,消息盒子无法移动了。
    box.resize(300,200);    //不起作用,原因未知。
    //设置窗口标题
    box.setWindowTitle("消息对话框");
    //设置标题
    box.setText(QString("<h3 style='text-align:center;'>温馨提示<h3>"));         //设置里面的Label的标题
    //设置新消息
    box.setInformativeText("你应该马上去学习!!!,否则你会落后。。。");  //Label里面的信息
    //设置按钮类型
    box.setStandardButtons(QMessageBox::Ok | QMessageBox::No);
    //设置无按钮
    box.setStandardButtons(QMessageBox::NoButton);
    //按钮默认选中
    box.setDefaultButton(QMessageBox::No);
    //设置显示信息图标
    //box.setIcon(QMessageBox::Icon::Information);
    box.setIcon(QMessageBox::Icon::NoIcon);

    //添加自定义按钮,可以通过样式表设置按钮和标签的颜色。
    QPushButton *okButton = box.addButton(tr("确认"), QMessageBox::ButtonRole::ActionRole);
    //相当于占位符 没啥作用
    QPushButton *noneButton = box.addButton("              ",QMessageBox::ButtonRole::ActionRole);
    QPushButton *cancelButton = box.addButton("取消",QMessageBox::ButtonRole::RejectRole);

    //方式1
    okButton->setStyleSheet("QPushButton{padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png') 20 20 20 20 stretch stretch;}"
                     "QPushButton:pressed{padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png') 20 20 20 20 stretch stretch;}");
    cancelButton->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png');}"
                     "QPushButton:pressed{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png');}");
    //文本虽然看不见,但按钮确认存在,需要设置不可点击。
    noneButton->setEnabled(false);
    noneButton->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:none;}");

//    //尝试设置按钮的样式(方式2) 不用定义指针 不用担心内存回收
//    foreach(auto i,box.buttons()){
    
    
//        qDebug()<<"i = "<<i->text();
//        if (i->text() == "确认") {
    
    
//            i->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png');}"
//                             "QPushButton:pressed{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png');}");
//        }
//        else if (i->text() == "取消") {
    
    
//            i->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png');}"
//                             "QPushButton:pressed{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png');}");
//        }
//        else {
    
    
//            i->setEnabled(false);
//            i->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;}");
//        }
//    }

    box.setStyleSheet("QMessageBox{background-color: rgb(0, 170, 255);color:#ffffff;border-radius:15px;}"
                      "QLabel{border-radius:15px;font-size:18px;min-width:150px;qproperty-alignment:AlignCenter;color:#ffffff;}"
                      );

    box.exec(); //模态事件一直循环,等待用户点击后响应。

    if (box.clickedButton() == okButton) {
    
    
        // do something
        qDebug()<<"确认";
    } else if (box.clickedButton() == cancelButton) {
    
    
        qDebug()<<"取消";
        // do something
    }
    else if (box.clickedButton() == noneButton) {
    
    
         //don't do
        qDebug()<<"关闭";
    }

    box.close();
}

insert image description here

QMessageBox (pointer object) recommends using this

A message box pointer object is created in the header file, and it can be created where it is actually used. It is created here because if there are many prompt boxes to be displayed in the project, the message box can be used during the entire class existence
. You can also encapsulate all relevant prompt boxes into a function, and display the message box by referencing and passing parameters, which can reduce the amount of code and improve efficiency.
insert image description here
function implementation
border-hover.png and border-pressed.png are the pictures of the button mouse hovering and pressing states respectively. 10 20 10 20 indicates the border width in the four directions of the button, and stretch stretch indicates how the image should be stretched to fit the new size (horizontal and vertical stretching) when the size of the button changes.

void MainWindow::on_PB_struct_2_clicked()
{
    
    
    qDebug()<<"================方法2======================";
    Pmsgbox = new QMessageBox;
    //去除标题和框
    Pmsgbox->setWindowFlag(Qt::FramelessWindowHint); //对话框无法在移动
    Pmsgbox->resize(300,200);    //不起作用
    //设置窗口标题
    Pmsgbox->setWindowTitle("消息对话框");
    //设置标题
    Pmsgbox->setText(QString("<h3 style='text-align:center;'>温馨提示<h3>"));         //设置里面的Label的标题
    //设置新消息
    Pmsgbox->setInformativeText("在上面的代码中,border.png是按钮边框的正常状态下的图片,"
                                "border-hover.png和border-pressed.png分别是按钮鼠标悬停和按压状态下的图片。"
                                "10 20 10 20表示在按钮四个方向上的边框宽度,stretch stretch表示当按钮的大小改变时,"
                                "图片应该如何拉伸来适应新的尺寸。"
                                "通过这种方式,你可以轻松地设置QMessageBox中按钮的外观和交互效果。");  //Label里面的信息
    //设置按钮类型
    Pmsgbox->setStandardButtons(QMessageBox::Ok | QMessageBox::No);
    //设置无按钮
    Pmsgbox->setStandardButtons(QMessageBox::NoButton);
    //按钮默认选中
    Pmsgbox->setDefaultButton(QMessageBox::No);
    //设置显示信息图标
    //box.setIcon(QMessageBox::Icon::Information);  //根据需要显示对应的图片
    Pmsgbox->setIcon(QMessageBox::Icon::NoIcon);
    //设置盒子固定大小,不会随文本自动调整
    Pmsgbox->setFixedSize(300,150); //无效,不知道为啥

    //添加自定义按钮,可以通过样式表设置按钮和标签的颜色。
    QPushButton *okButton = Pmsgbox->addButton(tr("确认"), QMessageBox::ButtonRole::ActionRole);
    //相当于占位符 没啥作用
    QPushButton *noneButton = Pmsgbox->addButton("              ",QMessageBox::ButtonRole::ActionRole);
    QPushButton *cancelButton = Pmsgbox->addButton("取消",QMessageBox::ButtonRole::RejectRole);

    //方式1
    okButton->setStyleSheet("QPushButton{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png') 20 20 20 20 stretch stretch;}"
                     "QPushButton:pressed{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png') 20 20 20 20 stretch stretch;}");
    cancelButton->setStyleSheet("QPushButton{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png') 20 20 20 20 stretch stretch;}"
                     "QPushButton:pressed{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png') 20 20 20 20 stretch stretch;}");
    //文本虽然看不见,但按钮确认存在,需要设置不可点击。
    noneButton->setEnabled(false);
    noneButton->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:none;}");

    //盒子宽高无效
    Pmsgbox->setStyleSheet("QMessageBox{background-color: rgb(0, 170, 255);color:#ffffff;border-radius:15px;}"
                      "QLabel{border-radius:15px;font-size:18px;min-width:150px;min-height:80px;qproperty-alignment:AlignCenter;color:#ffffff;}"
                      );

    //不可以。QMessageBox是一种模态对话框,当它被显示时,其他界面控件会被禁用,直到用户关闭该对话框。
    Pmsgbox->exec(); //模态事件一直循环,等待用户点击后响应。
    // Pmsgbox->setVisible(true);  //还是无法点击其他按钮
    //Pmsgbox->show();

    //您可以在显示QMessageBox的同时,使用QApplication::processEvents()函数处理其他事件,这样就可以让其他按钮被点击。例如:
   // QApplication::processEvents();//还是无效

    if (Pmsgbox->clickedButton() == okButton) {
    
    
        // do something
        qDebug()<<"确认";
    } else if (Pmsgbox->clickedButton() == cancelButton) {
    
    
        qDebug()<<"取消";
        // do something
    }
    else if (Pmsgbox->clickedButton() == noneButton) {
    
    
         //don't do
        qDebug()<<"关闭";
    }

    //Pmsgbox->close();
}

running result
insert image description here

on mouse button
insert image description here


QMessageBox Disadvantages

Maybe his shortcomings are his strengths! ! !

[1] The displayed boxes are all modal, no matter which function is displayed, that is, when this box is displayed, other buttons on the interface cannot be clicked. In large-scale projects, multi-threading of internal codes, connections between signals and slots , it is unknown whether there will be any impact. That is, you have to finish this thing before you can move on to the next thing.
[2] Setting the button style and text style can only be set through the style sheet.

Just record it, big brother don't spray

source code

head File

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QDebug>


QT_BEGIN_NAMESPACE
namespace Ui {
    
     class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    
    
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_PB_mBox_clicked();
    void on_PB_warning_clicked();
    void on_PB_error_clicked();
    void on_PB_struct_clicked();
    void on_PB_abort_clicked();
    void on_PB_quest_clicked();
    void on_toolButton_clicked();
    void on_PB_struct_2_clicked();
private:
    Ui::MainWindow *ui;
    QMessageBox *Pmsgbox = nullptr;
};
#endif // MAINWINDOW_H

Source File

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}


void MainWindow::on_PB_mBox_clicked()
{
    
    
    QMessageBox::information(this,"infomation","信息");
}

void MainWindow::on_PB_warning_clicked()
{
    
    
    QMessageBox::warning(this,"warning","信息");
}

void MainWindow::on_PB_error_clicked()
{
    
    
    QMessageBox::critical(this,"critical","信息");
}
void MainWindow::on_PB_abort_clicked()
{
    
    
     QMessageBox::about(this,"about","信息");

     QMessageBox::aboutQt(this,"aboutQt");
}
void MainWindow::on_PB_quest_clicked()
{
    
    
    QMessageBox::question(this,"question","信息");
}
void MainWindow::on_PB_struct_clicked()
{
    
    
    QMessageBox box;
    //去除标题和框
    box.setWindowFlag(Qt::FramelessWindowHint);
    box.resize(300,200);    //不起作用
    //设置窗口标题
    box.setWindowTitle("消息对话框");
    //设置标题
    box.setText(QString("<h3 style='text-align:center;'>温馨提示<h3>"));         //设置里面的Label的标题
    //设置新消息
    box.setInformativeText("你应该马上去学习!!!,否则你会落后。。。");  //Label里面的信息
    //设置按钮类型
    box.setStandardButtons(QMessageBox::Ok | QMessageBox::No);
    //设置无按钮
    box.setStandardButtons(QMessageBox::NoButton);
    //按钮默认选中
    box.setDefaultButton(QMessageBox::No);
    //设置显示信息图标
    //box.setIcon(QMessageBox::Icon::Information);
    box.setIcon(QMessageBox::Icon::NoIcon);

    //添加自定义按钮,可以通过样式表设置按钮和标签的颜色。
    QPushButton *okButton = box.addButton(tr("确认"), QMessageBox::ButtonRole::ActionRole);
    //相当于占位符 没啥作用
    QPushButton *noneButton = box.addButton("              ",QMessageBox::ButtonRole::ActionRole);
    QPushButton *cancelButton = box.addButton("取消",QMessageBox::ButtonRole::RejectRole);

    //方式1
    okButton->setStyleSheet("QPushButton{padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png') 20 20 20 20 stretch stretch;}"
                     "QPushButton:pressed{padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png') 20 20 20 20 stretch stretch;}");
    cancelButton->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png');}"
                     "QPushButton:pressed{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png');}");
    //文本虽然看不见,但按钮确认存在,需要设置不可点击。
    noneButton->setEnabled(false);
    noneButton->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:none;}");

//    //尝试设置按钮的样式(方式2) 不用定义指针 不用担心内存回收
//    foreach(auto i,box.buttons()){
    
    
//        qDebug()<<"i = "<<i->text();
//        if (i->text() == "确认") {
    
    
//            i->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png');}"
//                             "QPushButton:pressed{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png');}");
//        }
//        else if (i->text() == "取消") {
    
    
//            i->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png');}"
//                             "QPushButton:pressed{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png');}");
//        }
//        else {
    
    
//            i->setEnabled(false);
//            i->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;}");
//        }
//    }

    box.setStyleSheet("QMessageBox{background-color: rgb(0, 170, 255);color:#ffffff;border-radius:15px;}"
                      "QLabel{border-radius:15px;font-size:18px;min-width:150px;qproperty-alignment:AlignCenter;color:#ffffff;}"
                      );

    box.exec(); //模态事件一直循环,等待用户点击后响应。

    if (box.clickedButton() == okButton) {
    
    
        // do something
        qDebug()<<"确认";
    } else if (box.clickedButton() == cancelButton) {
    
    
        qDebug()<<"取消";
        // do something
    }
    else if (box.clickedButton() == noneButton) {
    
    
         //don't do
        qDebug()<<"关闭";
    }

    box.close();
}

void MainWindow::on_PB_struct_2_clicked()
{
    
    
    qDebug()<<"================2";
    Pmsgbox = new QMessageBox;
    //去除标题和框
    Pmsgbox->setWindowFlag(Qt::FramelessWindowHint);
    Pmsgbox->resize(300,200);    //不起作用
    //设置窗口标题
    Pmsgbox->setWindowTitle("消息对话框");
    //设置标题
    Pmsgbox->setText(QString("<h3 style='text-align:center;'>温馨提示<h3>"));         //设置里面的Label的标题
    //设置新消息
    Pmsgbox->setInformativeText("在上面的代码中,border.png是按钮边框的正常状态下的图片,"
                                "border-hover.png和border-pressed.png分别是按钮鼠标悬停和按压状态下的图片。"
                                "10 20 10 20表示在按钮四个方向上的边框宽度,stretch stretch表示当按钮的大小改变时,"
                                "图片应该如何拉伸来适应新的尺寸。"
                                "通过这种方式,你可以轻松地设置QMessageBox中按钮的外观和交互效果。");  //Label里面的信息
    //设置按钮类型
    Pmsgbox->setStandardButtons(QMessageBox::Ok | QMessageBox::No);
    //设置无按钮
    Pmsgbox->setStandardButtons(QMessageBox::NoButton);
    //按钮默认选中
    Pmsgbox->setDefaultButton(QMessageBox::No);
    //设置显示信息图标
    //box.setIcon(QMessageBox::Icon::Information);
    Pmsgbox->setIcon(QMessageBox::Icon::NoIcon);
    //设置盒子固定大小,不会随文本自动调整
    Pmsgbox->setFixedSize(300,150); //无效

    //添加自定义按钮,可以通过样式表设置按钮和标签的颜色。
    QPushButton *okButton = Pmsgbox->addButton(tr("确认"), QMessageBox::ButtonRole::ActionRole);
    //相当于占位符 没啥作用
    QPushButton *noneButton = Pmsgbox->addButton("              ",QMessageBox::ButtonRole::ActionRole);
    QPushButton *cancelButton = Pmsgbox->addButton("取消",QMessageBox::ButtonRole::RejectRole);

    //方式1
    okButton->setStyleSheet("QPushButton{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png') 20 20 20 20 stretch stretch;}"
                     "QPushButton:pressed{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png') 20 20 20 20 stretch stretch;}");
    cancelButton->setStyleSheet("QPushButton{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_7_d.png') 20 20 20 20 stretch stretch;}"
                     "QPushButton:pressed{border-radius:10px;padding:10px;color: rgb(255, 255, 255);font-size:30px;border-image:url(':/pic/button_10_d.png') 20 20 20 20 stretch stretch;}");
    //文本虽然看不见,但按钮确认存在,需要设置不可点击。
    noneButton->setEnabled(false);
    noneButton->setStyleSheet("QPushButton{border-radius:15px;color: rgb(255, 255, 255);font-size:30px;border-image:none;}");

    //盒子宽高无效
    Pmsgbox->setStyleSheet("QMessageBox{background-color: rgb(0, 170, 255);color:#ffffff;border-radius:15px;}"
                      "QLabel{border-radius:15px;font-size:18px;min-width:150px;min-height:80px;qproperty-alignment:AlignCenter;color:#ffffff;}"
                      );

    //不可以。QMessageBox是一种模态对话框,当它被显示时,其他界面控件会被禁用,直到用户关闭该对话框。
    Pmsgbox->exec(); //模态事件一直循环,等待用户点击后响应。
    // Pmsgbox->setVisible(true);  //还是无法点击其他按钮
    //Pmsgbox->show();

    //您可以在显示QMessageBox的同时,使用QApplication::processEvents()函数处理其他事件,这样就可以让其他按钮被点击。例如:
   // QApplication::processEvents();//还是无效

    if (Pmsgbox->clickedButton() == okButton) {
    
    
        // do something
        qDebug()<<"确认";
    } else if (Pmsgbox->clickedButton() == cancelButton) {
    
    
        qDebug()<<"取消";
        // do something
    }
    else if (Pmsgbox->clickedButton() == noneButton) {
    
    
         //don't do
        qDebug()<<"关闭";
    }

    //Pmsgbox->close();
}



void MainWindow::on_toolButton_clicked()
{
    
    
    for(int i = 0; i < 1000; ++i)
    {
    
    
        qDebug()<<"i = "<<i<<endl;
    }
}


resource
insert image description here


END

Guess you like

Origin blog.csdn.net/m0_45463480/article/details/131037600