当前位置:网站首页>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 :

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 :


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
边栏推荐
- Crawler for querying nicknames and avatars based on qqwebapi
- Generate verification code
- 深度学习经典网络解析目标检测篇(一):R-CNN
- Install the yapiupload plug-in in idea and upload the API interface to the Yapi document
- Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
- Permission management with binary
- 消费者灰度实现思路
- Multifunctional toolbox wechat applet source code
- Serial port debugging tools cutecom and minicom
- proxy server
猜你喜欢

Solving the problem of displaying too many unique values in ArcGIS partition statistics failed

SSD硬盘SATA接口和M.2接口区别(详细)总结

Robocode tutorial 3 - Robo machine analysis

Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck

Notepad + + replaces tabs with spaces

Gobang game based on pyGame Library

由tcl脚本生成板子对应的vivado工程

PowerDesigner various font settings; Preview font setting; SQL font settings

JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
![[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)](/img/ae/cbfc01fbcc816915b1794a9d70247a.png)
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
随机推荐
Log4j2 cross thread print traceid
Classes and objects
Calculation of fishing net road density
Robocode tutorial 8 - advanced robot
Imx6 debugging LVDS screen technical notes
Mysqldump backup database
proxy server
How to install jsonpath package
Cells in rust share variable pointers
WiFi ap6212 driver transplantation and debugging analysis technical notes
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
线上怎么确定期货账户安全的?
Re expression régulière
ArcGIS table to excel exceeds the upper limit, conversion failed
Win1远程出现“这可能是由于credssp加密oracle修正”解决办法
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
Spark performance optimization guide
Secure credit
A few lines of code teach you to crawl lol skin pictures