四川省工程建设协会网站,网站模板目录扫描,由于建设网站需要,河北省住房和城市建设局采购网站按钮-点击-窗口-关闭窗口
connect(信号的发送者#xff0c;发送具体信号#xff0c;信号的接收者#xff0c;信号的处理);
信号处理函数称为槽
信号槽的优点#xff0c;松散耦合#xff0c;信号发送端和接收端本身是没有关联的#xff0c;通过connect连接…按钮-点击-窗口-关闭窗口
connect(信号的发送者发送具体信号信号的接收者信号的处理);
信号处理函数称为槽
信号槽的优点松散耦合信号发送端和接收端本身是没有关联的通过connect连接将两端耦合在一起
//点击按钮关闭当前窗口
connect(myBtn, QPushButton::clicked, this, QWidget::close);
自定义信号和槽
teacher.h
#ifndef TEACHER_H
#define TEACHER_H#include QObjectclass 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 QObjectclass 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 iostream
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 QWidget
#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.hmyWidget::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 QMenuBar
#include QToolBar 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 // 模态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 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);