按钮->点击->窗口->关闭窗口
connect(信号的发送者,发送具体信号,信号的接收者,信号的处理);
信号处理函数称为槽
信号槽的优点,松散耦合,信号发送端和接收端本身是没有关联的,通过connect连接将两端耦合在一起
//点击按钮,关闭当前窗口
connect(myBtn, &QPushButton::clicked, this, &QWidget::close);
自定义信号和槽
teacher.h
#ifndef TEACHER_H
#define TEACHER_H#include class Teacher : public QObject
{Q_OBJECT
public:explicit Teacher(QObject *parent = nullptr);//自定义信号写到signals下//返回值类型为void,只需要声明,不需要实现//可以有参数,可以重载
signals:void hungry();};#endif // TEACHER_H
student.h
#ifndef STUDENT_H
#define STUDENT_H#include class Student : public QObject
{Q_OBJECT
public:explicit Student(QObject *parent = nullptr);//可以写到public下//返回值类型为void,需要声明,也需要实现//可以有参数,也可以重载void please();signals:};#endif // STUDENT_H
student.cpp
#include "student.h"
#include
Student::Student(QObject *parent) : QObject(parent)
{}
void Student::please()
{std::cout << "please teacher eat dinner" << std::endl;
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H#include
#include "teacher.h"
#include "student.h"
class myWidget : public QWidget
{Q_OBJECTpublic:myWidget(QWidget *parent = nullptr);Teacher *t = new Teacher(this);Student *s = new Student(this);~myWidget();
};
#endif // MYWIDGET_H
mywidegt.cpp
回调函数:在函数内部将被调函数名转换为地址作为参数供系统调用
信号可以连接信号
信号与槽的连接可以是一对多、多对一
信号的参数可以比槽多,但对应的参数在类型和顺序上要一致 (对于不匹配的,可以用匿名函数来调用相应的槽)
匿名函数的使用
btn->show();connect(btn, &QPushButton::clicked, this, [=](){QWidget *window1 = new QWidget;window1->show();btn->setText("close");});
#include "mywidget.h"myWidget::myWidget(QWidget *parent): QWidget(parent)
{connect(this->t, &Teacher::hungry, this->s, &Student::please);emit(this->t->hungry());// 重载时,通过函数指针传递函数地址
// //定义某类中函数的函数指针,要加类名以表作用域
// void (Teacher:: *hungryptr) (std::string name) = &Teacher::hungry;
// void (Student:: *pleaseptr) (std::string name) = &Student::please;
// connect(this->t, hungryptr, this->s, pleaseptr);
// emit(this->t->hungry("good food"));}myWidget::~myWidget()
{
}
菜单栏与工具栏
#include
#include
QMenuBar *menubar = new QMenuBar(this); //菜单栏QMenu *menu1 = new QMenu("file"); // 菜单menu1->addAction("new");menu1->addAction("open");menubar->addMenu(menu1);menubar->move(200, 300);QToolBar *toolbar = new QToolBar(this); // 工具栏toolbar->addAction("new");toolbar->addAction("open");
自定义对话框
模态对话框:不可以对其他窗口进行操作
非模态对话框:可以对其他窗口进行操作
#include
// 模态QDialog log1(this);log1.resize(100, 200);log1.exec();// 非模态,用指针建立,存储在堆上,如果存在栈上匿名函数执行完就释放了// 会出现一闪而过的情况,模态的不需要QDialog *log2 = new QDialog(this);log2->resize(100, 200);log2->show();log2->setAttribute(Qt::WA_DeleteOnClose); //关闭时释放内存,不然会内存泄漏
标准对话框
#include
QMessageBox::critical(this, "critical", "out of range");QMessageBox::information(this, "info", "text");// QMessageBox::question(this, "query", "answer", QMessageBox::Save | QMessageBox::No);if( QMessageBox::question(this, "query", "answer", QMessageBox::Save | QMessageBox::No) == QMessageBox::Save){std::cout << "select Save" << std::endl;}else{std::cout << "select No" << std::endl;}
布局
水平布局、垂直布局、栅格布局
使用时可以直接创建相应布局,将需要布局的item放入即可
或者创建Widget,放入item并选择相应布局
使用Horizontal spacer 或者 Vertical spacer来将label、按钮、编辑框等自适应窗口大小
QStringList
QListWidgetItem *item = new QListWidgetItem("1111");QStringList strs; // string liststrs << "111" << "222" << "333";ui->listWidget->addItem(item);ui->listWidget->addItems(strs);