当前位置:网站首页>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
边栏推荐
- Rust: a simple example of TCP server and client
- Selenium + phantom JS crack sliding verification 2
- Batch export ArcGIS attribute table
- Dynamically add default fusing rules to feign client based on sentinel + Nacos
- Flash - Middleware
- Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
- What are the relationships and differences between threads and processes
- The vivado project corresponding to the board is generated by TCL script
- How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
- NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
猜你喜欢
多功能工具箱微信小程序源码
From introduction to mastery of MATLAB (2)
深度学习经典网络解析目标检测篇(一):R-CNN
PowerDesigner various font settings; Preview font setting; SQL font settings
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
C medium? This form of
随机推荐
Selenium + webdriver + chrome realize Baidu to search for pictures
Software test summary
Rust: the output information of println is displayed during the unit test
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
Docker installation MySQL
Stm32mp157 wm8960 audio driver debugging notes
Mysqldump backup database
解决允许在postman中写入注释请求接口方法
Resolves the interface method that allows annotation requests to be written in postman
【ACM】70. climb stairs
Qt读写XML文件(含源码+注释)
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
Notepad + + replaces tabs with spaces
Calculation of fishing net road density
PowerDesigner various font settings; Preview font setting; SQL font settings
Creation and use of QT dynamic link library
Excel opens large CSV format data
【ACM】509. Fibonacci number (DP Trilogy)
Rust: shared variable in thread pool
STM32学习记录0008——GPIO那些事1