当前位置:网站首页>Qt自定义类使用自定义含参信号与槽
Qt自定义类使用自定义含参信号与槽
2022-07-24 03:05:00 【谛听misa】
最近在自定义类中使用了信号来连接到MainWindow类中的槽函数,具体操作如下:
要点一:继承QObject
在头文件中初始化自定义类MyThread1,这里由于继承的QThread已经继承了QObject,无需嵌套继承,故不必public QObject,但仍需加入Q_OBJECT来保证moc编译器可以连接信号和槽函数
//位置:mainwindow.h
//一个线程类的实例,单纯使用自定义信号的话除了signals以外的部分都不用关心
class MyThread1 : public QThread
{
Q_OBJECT
public:
MyThread1();
void stop();
QTcpSocket* position;
bool pos_connected;
signals:
void pos_update(QString x,QString y);
protected:
void run();
private:
volatile bool stopped;
};
要点二:自定义信号与槽
在自定义的类里面声明信号pos_update(QString x,QString y),但其只是信号,无需在cpp文件中给出实现
在MainWindow中加入信号对应的槽函数
注意:信号与槽的参数数量、类型与顺序要保持一致(可以出现信号参数比槽函数多,但也不建议这么做,反之槽函数参数比信号参数多则是绝对不行的)至于使用自定义的数据类型作为信号参数可以参考链接
槽函数必须给出实现:
//位置:mainwindow.cpp
//接收另一个线程传来的消息后将其展示在ui界面上
void MainWindow::update(QString x,QString y)
{
ui->pos_x->setText(x);
ui->pos_y->setText(y);
}
要点三:连接信号与槽
实例化类时最好使用指针,便于信号连接,下图中有两种信号连接的表达方式,本质上都是connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method,
Qt::ConnectionType type = Qt::AutoConnection)
更多连接写法见[链接](https://blog.csdn.net/qq_27898413/article/details/11967
7345)
//位置:mainwindow.cpp声明的全局变量
MyThread1 *thread1 = new MyThread1();
//位置:mainwindow.cpp中MainWinw类的初始化函数中
connect(thread1,&MyThread1::pos_update,this,&MainWindow::update);
要点四:发送信号
直接在需要发送信号的地方emit即可
//位置:mainwindow.cpp中任何需要调用update的地方
emit pos_update(posx,posy);
边栏推荐
- PMP first-hand data and information acquisition
- The next stop of data visualization platform | gifts from domestic open source data visualization datart "super iron powder"
- CMT 注册——Google Scholar Id,Semantic Scholar Id,和 DBLP Id
- 老公,我们现在无家可归了
- Diversity of SIGIR '22 recommendation system papers
- 攻防世界WEB练习区(webshell、command_execution、simple_js)
- 软考---程序设计语言基础(上)
- Summernote rich text editor
- 【HDLBits 刷题】Verilog Language(2)Vectors 部分
- Recorded on July 21, 2022
猜你喜欢

To forge ahead on a new journey, the city chain science and technology carnival was grandly held in Xiamen

Ugui source code analysis - Mask
[email protected] Principle of use"/>(6) Decorator extension [email protected] Principle of use
![SSM based blog system [with background management]](/img/6b/6a488f5d6926de07c8b1b365362ff6.png)
SSM based blog system [with background management]

How to get gait energy map Gei

Attack and defense world web practice area (weak_auth, simple_php, xff_referer)

SSM家庭理财个人理财管理系统记账系统

198. House raiding

508. The subtree element with the most occurrences and the pure C implementation of hash table method

Ugui source code analysis - imaskable
随机推荐
PMP preparation experience | good habits, good process, good results
uva1445
Ugui source code analysis - imaterialmodifier
老公,我们现在无家可归了
动态规划-01背包问题
Binary tree traversal (day 74)
Basic knowledge of trigger (Part 2)
LCD1602 - binge 51
Unity 消息推送
MySQL sub database and sub table and its smooth expansion scheme
Why use the well architected framework?
LeetCode-栈和队列刷题
Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
Attack and defense world web practice area (weak_auth, simple_php, xff_referer)
Ugui source code analysis - imaskable
TCP connection principle
攻防世界WEB練習區(view_source、get_post、robots)
Attack and defense world web practice area (backup, cookie, disabled_button)
攻防世界WEB练习区(view_source、get_post、robots)
go log包