Qt 自定義QMessageBox
阿新 • • 發佈:2020-12-30
技術標籤:qt
自定義QMessageBox
參考qt自帶的 QMessageBox原始碼,通過繼承QDialog純程式碼實現。程式碼結構清晰,原理簡單,複製直接可用,不再進行太多理論講解。
效果圖
直接上效果圖:
程式碼實現
直接上程式碼
messagebox.h 標頭檔案
#ifndef MESSAGEBOX_H #define MESSAGEBOX_H #include <QWidget> #include <QDialog> #include <QLabel> #include <QPushButton> #include <QDebug> #include <QString> enum MsgBoxType { MsgBoxType_Warn = 0, MsgBoxType_Info = 1, MsgBoxType_Error = 2 }; class MessageBox : public QDialog { Q_OBJECT public: explicit MessageBox(QWidget *parent, MsgBoxType type, QString text); void initState(); void initWarn(const QString &text); void initError(const QString &text); void initInfo(const QString &text); signals: public slots: void dealbtnSureClicked(); void dealbtnCancelClicked(); private: QLabel *labPic; QLabel *labNote; QPushButton *btnSure; QPushButton *btnCancle; }; #endif // MESSAGEBOX_H
messagebox.cpp 實現檔案
#include "messagebox.h" MessageBox::MessageBox(QWidget *parent, MsgBoxType type, QString text) : QDialog(parent) { initState(); if(type == MsgBoxType_Info) { initInfo(text); } else if(type == MsgBoxType_Warn) { initWarn(text); } else { initError(text); } } void MessageBox::initState() { this->resize(240,160); this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog); this->setStyleSheet("background-color:rgb(46,47,48)"); labPic = new QLabel(this); labNote = new QLabel(this); btnSure = new QPushButton("確認",this); btnCancle = new QPushButton("取消",this); connect(btnSure,&QPushButton::clicked,this,&MessageBox::dealbtnSureClicked); connect(btnCancle,&QPushButton::clicked,this,&MessageBox::dealbtnCancelClicked); } void MessageBox::initWarn(const QString &text) { int width = this->width(); labPic->setStyleSheet("image:url(:/image/msg_question.png)"); labPic->setGeometry(width*0.5-20,10,40,40); labNote->setStyleSheet("color:white"); labNote->setAlignment(Qt::AlignCenter); labNote->setGeometry(0,70,width,20); labNote->setText(text); btnSure->setGeometry(width*0.2-15,110,80,30); btnSure->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}" "QPushButton:hover{background-color:blue}" "QPushButton:pressed{background-color:blue}"); btnCancle->setGeometry(width*0.6,110,80,30); btnCancle->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}" "QPushButton:hover{background-color:blue}" "QPushButton:pressed{background-color:blue}"); } void MessageBox::initError(const QString &text) { int width = this->width(); labPic->setStyleSheet("image:url(:/image/msg_error.png)"); labPic->setGeometry(width*0.5-20,10,40,40); labNote->setStyleSheet("color:white"); labNote->setAlignment(Qt::AlignCenter); labNote->setGeometry(0,70,width,20); labNote->setText(text); btnSure->setGeometry(width*0.5-40,110,80,30); btnSure->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}" "QPushButton:hover{background-color:blue}" "QPushButton:pressed{background-color:blue}"); btnCancle->hide(); } void MessageBox::initInfo(const QString &text) { int width = this->width(); labPic->setStyleSheet("image:url(:/image/msg_info.png)"); labPic->setGeometry(width*0.5-20,10,40,40); labNote->setStyleSheet("color:white"); labNote->setAlignment(Qt::AlignCenter); labNote->setGeometry(0,70,width,20); labNote->setText(text); btnSure->setGeometry(width*0.5-40,110,80,30); btnSure->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}" "QPushButton:hover{background-color:blue}" "QPushButton:pressed{background-color:blue}"); btnCancle->hide(); } void MessageBox::dealbtnSureClicked() { this->accept(); } void MessageBox::dealbtnCancelClicked() { this->reject(); }
函式呼叫
MessageBox msgBox(this, MsgBoxType_Warn, "警告資訊"); int res = msgBox.exec(); if(res == QDialog::Accepted) { qDebug() << "Accepted"; } else if(res == QDialog::Rejected) { qDebug() << "Rejected"; } else { qDebug() << "error"; }
總結
實現思路,其實就是建立一個繼承QDialog的widget而已,QDialog使其有自帶messageBox的屬性,widget佈局自己發揮。