The QT flexible message box (may be used in any class)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Sun_tian/article/details/102756010

We know, QT QMainWindow in the class or classes and other QDialog can use a message box pops up user prompts or warnings. But in some class itself defined, QMessagBox class is not available for this problem, QT signal slots mechanism can borrow design a pop mechanism.
Effect: You can use emit any class customized pop (pop stuck, or shut down automatically after xxms)
involves knowledge: Create a signal slot, bind, issue; custom pop category, text content, staying duration;

Man of few words said, directly on the code

First, we define classes that are needed emit pop in this class

test.h file

class Test : public QObject
{
    Q_OBJECT
public:
    explicit  Test();
    ~ Test();  
    void emit_MessBox();  
signals:
    void MessBox(QMessageBox::Icon,QString,int); //参数为消息框类型、文本内容、停留时间(ms)
}

Test class inherits from QObject and macros for use Q_OBJECT declaration signal slots mechanism in the Test class

test.cpp file (excerpt)

void Test::emit_MessBox()
{
    emit MessBox(QMessageBox::Warning,"这是一个测试弹窗",0);
}

In emit_MessBox function, use emit signals, frame type bomb alert box, the text reads "This is a test pop", the residence time is 0ms (represented remained, until the user clicks it closed).

A, the MainWindow class, need to implement the function corresponding to the grooves in this class, pop operations performed

mainwindow.h

#include "test.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    Ui::MainWindow *ui;
private slots:
    void MessBox(QMessageBox::Icon,QString,int);

}

mainwindow.cpp signal binding groove, the groove achieved


void MainWindow::SignalsConnect()
{
    qRegisterMetaType <QMessageBox::Icon> ("QMessageBox::Icon");  //注册参数类型 <QMessageBox::Icon>
    QObject::connect(T,SIGNAL(MessBox(QMessageBox::Icon,QString,int)),
            this,SLOT(MessBox(QMessageBox::Icon,QString,int)),Qt::QueuedConnection);
}

void MainWindow::MessBox(QMessageBox::Icon messboxIcon, QString str,int ms)
{
    if(ms==0)
    {
        if(messboxIcon==QMessageBox::Information)
            QMessageBox::information(this, tr("消息"),str,
                                           QMessageBox::Ok | QMessageBox::Cancel);
        else if(messboxIcon==QMessageBox::Warning)
            QMessageBox::warning(this, tr("警告"),str,
                                           QMessageBox::Ok );
        else if(messboxIcon==QMessageBox::Critical)
            QMessageBox::critical(this, tr("错误"), str,
                                           QMessageBox::Ok );
        else
            QMessageBox::question(this, tr("未知"),str,
                                           QMessageBox::Ok );
    }
    else if(ms>0)
    {
        QMessageBox* box;
        box = new QMessageBox("Messagebox",
                str,
                messboxIcon,
                QMessageBox::Ok | QMessageBox::Default,
                QMessageBox::Cancel | QMessageBox::Escape,
                0);

        box->show();
        Delay_MSec(ms);
        box->hide();
        delete box;
    }
}

NOTE: QObject :: Connect (T, the SIGNAL (MessBox (a QMessageBox :: Icon, QString, int)),
the this, the SLOT (MessBox (a QMessageBox :: Icon, QString, int)), the Qt :: QueuedConnection);
binding operation , T is the Test class pointer to a Test object.
Note: Before emit code is executed to ensure that the connection has been made, it is recommended that the signal slot connection class constructor MainWindow completed, emit signals in the Test class constructor completes, when required then perform pop.

Summary: QT signal slot is a convenient and secure mechanism, it can be used between different classes, as long as they inherit from QObject class, you can use signals and slots, and this mechanism is thread-safe, multi-threaded programming It provides a convenient aid. Have questions, please leave a message, progress together!

Guess you like

Origin blog.csdn.net/Sun_tian/article/details/102756010