当前位置:网站首页>QT basic functions, signals, slots
QT basic functions, signals, slots
2022-08-01 17:38:00 【little engineer】
创建QT项目名不能有空格和中文,The project path cannot have a Chinese path!!!
基本控件介绍
QT基本控件
QLabel: 标签,Text information can be displayed,只读;
QPushButton: 普通按钮;
QRadioButton: 单选按钮,Only one of the multiple radio buttons can be selected,But it has to be put in
groupbox 中,Similar to multiple choice questions;
QCheckBox: Multi-select check button,You can choose to select multiple at the same time,Similar to multiple choice questions;
QLineEdit: 单行文本编辑框,A single line of text can be entered;
QTextEdit: 多行文本输入框,You can enter and display multiple lines of text and pictures;
QComboBox: Drop-down text input box,There is a triangular drop-down button on the far right of the input box,可以
选择输入,也可以手动输入;
QTextBrowser: Multi-line text display box,只读;
QGroupBox: You can put something special in it,统一管理;
QSlider: Simulate the display slider;
QSpinBox: Numerical display slider;
QDateEdit: 日期编辑框
QTimeEdit: 时间编辑框
QDateTimeEdit: 日期时间编辑框
QLCDNumber: LCD 数字控件
//Template creation of base objects
//.h文件
#ifndef BUTTON_H
#define BUTTON_H
#include <QWidget> //The initial creation is inherited asQWidget类,Because there are no optionsQPushButton作为父类
#include <QPushButton> //Inherited parent class header file added
//class button : public QWidget
class button : public QPushButton //public QWidget->Inherited modifications:public QPushButton
{
Q_OBJECT //It is determined that the signal slot mechanism can be used,可以生成moc文件
public:
explicit button(QWidget *parent = nullptr); //explicit
button();
~button();
signals:
};
#endif // BUTTON_H
//.c文件
#include "button.h"
#include <QDebug>
//button::button(QWidget *parent) : QWidget(parent) //:QWidget(parent) ->修改继承类 : QPushButton(parent)
button::button(QWidget *parent) : QPushButton(parent) // 传一个parent指针,交给QPushButton(parent)The parent class to construct
{
}
button::~button()
{
qDebug()<<"MyButton的析构";
}
对象树
对象树(Subobject dynamically allocated space does not need to be freed)
Qt 提供了一种机制,能够自动、有效的组织和管理继承自QObject 的Qt 对象,这种机制就是对象树.所有从QObject 或其子类(例如Qwidget)派生的类都能够包含信号和槽.
Qt Object trees are very useful in user interface programming.It can help programmers reduce the pressure of memory leaks.For example when an application creates an object with a parent widget,该对象将被加入父窗口部件的孩子列表.当应用程序销毁父窗口部件时,其下的孩子列表中的对象将被一一删除.这在编程时,能够将主要精力放在系统的业务上,提高编程效率,同时也保证了系统的稳健性.
信号槽
槽可以有参数,但槽的参数不能有缺省值.
槽函数分为三种类型,即public slots、private slots 和protected slots.
- public slots:在这个区内声明的槽意味着任何对象都可将信号与之相连接.
- protected slots:在这个区内声明的槽意味着当前类及其子类可以将信号与之相连接.
- private slots:在这个区内声明的槽意味着只有类自己可以将信号与之相连接.
- 发射信号时,接收者的槽函数将被调用.
1)一个信号可以连接到多个槽.
2)A slot can be connected by multiple signals.
3)Signals can also be connected to signals,At this time, the transmission signal of the former will lead to the transmission of the latter.
4)信号的参数类型可以与槽的参数类型对应,信号的参数可以比槽的参数多,但不可以少,否则连接将失败.
QT4写法:connect(button, SIGNAL(clicked()), &a, SLOT(quit()));
注意到connect()函数的signal 和slot 都是接受字符串,一旦出现连接不成功的情况,Qt4 是没有编译错误的(因为一切都是字符串,编译期是不检查字符串是否匹配),而是在运行时给出错误.这无疑会增加程序的不稳定性.
程序运行时,connect With the help of two strings,The association between the signal and the slot can be established,那么,it is if
做到的呢?C++experience can tell us:
- The class should hold string information about signals and slots
- Strings and signal and slot functions should be associated
而这,Just by magicImplemented by the meta-object system(Qt 的元对象系统预处理器叫做moc,对文件预处理之后生成一个moc_xxx.cpp 文件,然后和其他文件一块编译即可)
(下图来自网络)
基本使用方法
//头文件加入
Q_OBJECT //It is determined that the signal slot mechanism can be used,可以生成moc文件
//基本使用方式
QApplication app(argc, argv);
QPushButton button("Quit");
QObject::connect(&button, &QPushButton::clicked, &app, &QApplication::quit);
// Basic program fundamentals
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
//应用程序, a,QT中,有且仅有一个,应用程序对象
QApplication a(argc, argv);
Widget w;
w.show(); //The window does not pop up by default,如果想弹出 ,需要调用show方法
return a.exec(); //消息循环机制,pause机制,等同于whilealready judgedbreak;exec:execute
}
// 信号和槽
QPushButton * quitBtn = new QPushButton("关闭窗口",this);
// 信号和槽 connect(sender, signal, receiver, slot);
// !注意的是,sender和receiver都是指针对象,最好用new出来的对象!,或者采用&Obtain the object address
connect(quitBtn,&QPushButton::clicked,this,&MyWidget::close);
//自定义信号与槽
private signals:
void hungury();
public slots:
void treat();
// 信号槽连接
connect(teacher,&Teacher::hungury,student,&Student::treat);
//发送信号
emit teacher->hungury();
/*带参数的信号、槽*/
void hungury(QString name); 自定义信号
void treat(QString name ); 自定义槽
//但是由于有两个重名的自定义信号和自定义的槽,直接连接会报错,
//所以需要利用函数指针来指向函数地址, 然后再做连接
void (Teacher:: * teacherSingal)(QString) = &Teacher::hungury;
void (Student:: * studentSlot)(QString) = &Student::treat;
connect(teacher,teacherSingal,student,studentSlot);
// 也可以如此操作:
QObject::connect(this, //which object emits
SIGNAL(ViewImage1(cv::Mat)), //发出的信号.
ui.cameraShow,
SLOT(SetImage(cv::Mat))
);
//例子1
connect(ui.tests, SIGNAL(clicked()), this, SLOT(TestSlot()));
//例子2
testwidgetRect w;
QObject::connect(&xt, SIGNAL(Move(int, int)), &w, SLOT(move(int, int))); //Take the address method
//Lambda表达式:
connect(ui->checkBox,&QCheckBox::stateChanged,[=](int state){
qDebug()<<state; //未选中:0 选中:2 半选中:1 -->for total options,The options in the total options have some selected cases.
});
边栏推荐
猜你喜欢
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
05 Doris cluster construction
C# LibUsbDotNet 在USB-CDC设备的上位机应用
Unity ui点击事件只响应最上层ui的方式
[Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails
TCP百万并发服务器优化调参
SQL的索引详细介绍
千万级乘客排队系统重构&压测方案总结篇
QT_QThread线程
Sftp中文件名乱码
随机推荐
RecSys'22|CARCA: Cross-Attention-Aware Context and Attribute Recommendations
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证
XAML WPF item groupBox control
M1芯片电脑安装cerebro
生物制药产业发展现状和趋势展望
QT_QDialog 对话框
Unity ui点击事件只响应最上层ui的方式
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
金仓数据库 KDTS 迁移工具使用指南(2. 简介)
2022年SQL大厂高频实战面试题(详细解析)
Topology零部件拆解3D可视化解决方案
C语言理论--笔试面试基础稳固
03 gp cluster construction
自定义注解实现日志打印时屏蔽特定字段不打印
数字化采购管理系统开发:精细化采购业务流程管理,赋能企业实现“阳光采购”
[ACNOI2022]物品
2022年SQL经典面试题总结(带解析)
B002 - Embedded Elderly Positioning Tracking Monitor
想做期货,农产品期货怎么炒?波动大么