一、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;
}
输出: