当前位置:网站首页>Custom prompt box MessageBox in QT

Custom prompt box MessageBox in QT

2022-04-23 18:17:00 Talent、me

Qt in QMessageBox Use of message dialog box

Qt The built-in dialog box in QMessageBox

Add header file #include

Function name prototype :static StandardButton QMessageBox::information ( QWidget * parent, const QString & title, constQString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton );

# There are the following prompt box types :
Qmessagebox::question()
Qmessagebox::information()
Qmessagebox::warning()

# Examples are as follows :
// Get the returned button content
StandardButton::QMessageBox btn= QMessageBox::information(NULL, “Title”, “Content”, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(btn==QMQMessageBox::YES)
{
// Execute code content
}
else
{
// Execute code content
}
The effect is as follows :
 Insert picture description here

Custom prompt box

Why do you need to design a custom prompt box ?
One is :Qt The style of the built-in prompt box is single ;
Two is : The prompt box is too small ;
The third is : It doesn't match the project ;

The renderings are as follows :
 Insert picture description here
 Insert picture description here

The code is as follows :
messageBox.h

class messageBox:public QDialog
{
    
    Q_OBJECT
public:
    messageBox(QString title = " Tips ",QString text = nullptr, int showMessageType = Warning, int showBtnType = ButtonYes, QDialog *parent = nullptr);// Constructors 
    ~messageBox();
    enum Icon {
    // Icon 
        // keep this in sync with QMessageDialogOptions::Icon
        Information = 1,
        Warning = 2,
        Critical = 3,
        Question = 4
    };
	// Button selection 
    enum Button{
    
        ButtonYes = 0,	// One yes
        ButtonNo,		// One no
        ButtonYesAndNo,	// There are two buttons , One yes, One no
        ButtonNot,		// No buttons 
    };
private:
    QString m_title;		// The title bar 
    QString m_showText;		// Content 
    int m_showMessageType;	// Display type 
    int m_showBtnType;		// Button display type 

    QLabel *titleLabel;
    QLabel *showLabel;
    QLabel *iconLabel;
    QPushButton *okBtn;
    QPushButton *quitBtn;
   // QIcon *icon;
    QHBoxLayout *titlelayout;
    QHBoxLayout *layout;
    QHBoxLayout *layout1;
    QVBoxLayout *vLayout;
    void btnStyle();
    void messageStyle();
private slots:
    void btnClick();

};

messageBox.cpp

messageBox::messageBox(QString title, QString text,int showMessageType,int showBtnType,QDialog *parent)
    : QDialog(parent),
      m_title(title),
      m_showText(text),
      m_showMessageType(showMessageType),
      m_showBtnType(showBtnType)

{
    
    btnStyle();
    messageStyle();
    vLayout = new QVBoxLayout;
    vLayout->addLayout(titlelayout);
    vLayout->addLayout(layout);
    vLayout->addLayout(layout1);
    vLayout->setSpacing(0);
    vLayout->setMargin(0);
    vLayout->setStretch(0,1);
    vLayout->setStretch(1,2);
    vLayout->setStretch(2,1);
    this->resize(360,200);
    this->setLayout(vLayout);
    this->show();
    connect(okBtn,SIGNAL(clicked()),this,SLOT(btnClick()));
    connect(quitBtn,SIGNAL(clicked()),this,SLOT(btnClick()));

}

The button triggers the slot function
void messageBox::btnClick()
{
QPushButton * btn = (QPushButton*)sender();
if (btn == okBtn) {
this->accept(); // return 1
} else if (btn == quitBtn) {
this->reject(); // return 0
}
}

Layout of prompt box

void messageBox::btnStyle()
{
    
    titlelayout = new QHBoxLayout;
    titleLabel = new QLabel(m_title);
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setStyleSheet("background: rgbda(85,174,255,100%);color:#ffffff;");
    titlelayout->addWidget(titleLabel);
    
    okBtn = new QPushButton;
    quitBtn = new QPushButton;
    okBtn->setText(tr(" determine "));
    okBtn->setFixedSize(120,50);
    okBtn->setFocusPolicy(Qt::NoFocus);
    okBtn->setStyleSheet("border-image:url(:/image/partdialog/ Not selected .png);color: #ffffff;");
    quitBtn->setText(tr(" Cancel "));
    quitBtn->setFixedSize(120,50);
    quitBtn->setFocusPolicy(Qt::NoFocus);
    quitBtn->setStyleSheet("border-image:url(:/image/partdialog/ Not selected .png);color: #ffffff;");
    
    layout1 = new QHBoxLayout;
    if (m_showBtnType == ButtonYes) {
    
        layout1->addWidget(okBtn);
        layout1->setSpacing(30);
        layout1->setMargin(15);
    } else if (m_showBtnType == ButtonNo) {
    
        layout1->addWidget(quitBtn);
        layout1->setSpacing(30);
        layout1->setMargin(15);
    } else if (m_showBtnType == ButtonYesAndNo) {
    
        layout1->addWidget(okBtn);
        layout1->addWidget(quitBtn);
        layout1->setSpacing(30);
        layout1->setMargin(15);
    } else {
    

    }
}

Conclusion : In fact, the focus of the prompt box is to apply Dialog, And then with the trigger mechanism of the button ,accept() and reject() The return value of , When the main thread calls if (msg.exec()) Judge

版权声明
本文为[Talent、me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610470956.html