当前位置:网站首页>Qt:QMessageBox消息框、自定义信号和槽
Qt:QMessageBox消息框、自定义信号和槽
2022-07-28 03:23:00 【@布响丸辣】
一、QMessageBox消息框
#include "widget.h"
#include <QApplication>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
// QMessageBox::information(&w,"提示","做核酸");
// QMessageBox::StandardButton ret = QMessageBox::question(&w,"提示","做核酸了吗");
// if(ret == QMessageBox::Yes)
// {
// QMessageBox::information(&w,"提示","真棒");
// }
// else
// {
// QMessageBox::critical(&w,"提示","快去做");
// }
//
//
QMessageBox mb(QMessageBox::NoIcon,"提示","按一下",QMessageBox::Yes|QMessageBox::No,&w);
// QMessageBox mb;
//
// mb.setWindowTitle("提示");
// mb.setText("按一下");
//
// mb.addButton("提示",QMessageBox::AcceptRole);
// mb.addButton("错误",QMessageBox::Cancel);
w.show();
int ret = mb.exec();
if(ret == QMessageBox::AcceptRole)
{
QMessageBox::information(&w,"提示","真棒");
}
else
{
QMessageBox::critical(&w,"提示","快去做");
}
return a.exec();
}二、自定义信号和槽
1.去掉槽的参数
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void MySlot();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QObject::connect(ui->pushButton,&QPushButton::clicked,this,&Widget::MySlot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::MySlot()
{
ui->label->setText("大xx");
}
2.小实例
#include "student.h"
Student::Student(QObject *parent) : QObject(parent)
{
}
void Student::Treat()
{
QMessageBox::information(nullptr,"提示","请客吃饭");
}
#include "teacher.h"
Teacher::Teacher(QObject *parent) : QObject(parent)
{
}
void Teacher::ClassOver()
{
QMessageBox::information(nullptr,"提示","我饿了,下课吧");
emit Hungry();
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
pTe = new Teacher;
pSt = new Student;
QObject::connect(ui->pushButton,&QPushButton::clicked,pTe,&Teacher::ClassOver);
QObject::connect(pTe,&Teacher::Hungry,pSt,&Student::Treat);
}
Widget::~Widget()
{
delete ui;
delete pTe;
delete pSt;
}
输出:

边栏推荐
- Mouse operation and response
- What if the word selection box of win11 input method is missing?
- 如何一键进行重装Win11系统
- Redis communication protocol -- resp protocol
- Play WolframAlpha computing knowledge engine
- 光年(Light Year Admin)后台管理系统模板
- Light year admin background management system template
- 4天Excel实战训练营,0.01元特惠仅三天,赠200套学习资料包
- Win11输入法的选字框不见了怎么办?
- 动态规划——474. 一和零
猜你喜欢

How to solve the problem of win11 black desktop background?

Redis 5 kinds of data structure analysis

The open source of "avoiding disease and avoiding medicine" will not go far

每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

Shell:一键部署pxe

ES6 从入门到精通 # 09:Symbol 类型

IronOCR for .NET 2022.8

图文并茂:JVM 内存布局详解

Win11输入法的选字框不见了怎么办?

ES6 从入门到精通 # 08:扩展的对象的功能
随机推荐
AIRIOT答疑第6期|如何使用二次开发引擎?
Win11怎么显示固定应用?
D2dengine edible tutorial (4) -- draw text
"Xiaodeng" network equipment monitoring in operation and maintenance
Airiot Q & A issue 6 | how to use the secondary development engine?
Win11输入法的选字框不见了怎么办?
MySQL事务的ACID特性及并发问题实例分析
A treasure simulates login and reduces the method of secondary verification
Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management
【论文笔记】基于深度学习的移动机器人自主导航实验平台
Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
STM32 RT thread virtual file system mount operation
【5G NR】RRC Reject解析
Shell: one click deployment PXE
LabVIEW加载和使用树型控件项目中的定制符号
动态规划——62. 不同路径
过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
How to uninstall clean ZABBIX service? (super detailed)
Light year admin background management system template
关于湖北文理学院平衡信标组的疑问回应