当前位置:网站首页>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
边栏推荐
- JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
- C [file operation] read TXT text by line
- 【ACM】70. climb stairs
- Reptile efficiency improvement method
- Generate verification code
- Resolves the interface method that allows annotation requests to be written in postman
- Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
- Creation and use of QT dynamic link library
- Calculation of fishing net road density
- [UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
猜你喜欢

Cygwin64 right click to add menu, and open cygwin64 here

C medium? This form of

Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
![[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)](/img/07/4814eb203dcca59416a7997bbedbf6.png)
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)

Deep learning classic network analysis and target detection (I): r-cnn

Robocode Tutorial 4 - robocode's game physics

Solution to Chinese garbled code after reg file is imported into the registry

Jeecg boot microservice architecture
![Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]](/img/5f/a80951777a0473fcaa685cd6a8e5dd.png)
Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]

Resolves the interface method that allows annotation requests to be written in postman
随机推荐
WIN1 remote "this may be due to credssp encryption Oracle correction" solution
In win10 system, all programs run as administrator by default
Quantexa CDI(场景决策智能)Syneo平台介绍
Refcell in rust
Identification verification code
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
Matlab tips (6) comparison of seven filtering methods
Install the yapiupload plug-in in idea and upload the API interface to the Yapi document
Crawl lottery data
Array rotation
According to the result set queried by SQL statement, it is encapsulated as JSON
Rust: shared variable in thread pool
Mode of interprocess communication
powerdesigner各种字体设置;preview字体设置;sql字体设置
Classes and objects
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
Reptile efficiency improvement method
Jeecg boot microservice architecture
In shell programming, the shell file with relative path is referenced
Qtablewidget usage explanation