1、第一种实现方式
在QT开发工具UI界面先拖入按钮,然后鼠标右键拖入按钮,点击选中槽,在页面选着需要的信号,然后OK,随即将会跳转到类的.cpp文件,(这种UI+代码结合的方式,会自动去绑定信号与槽)。
2、第二种实现方法
纯代码实现
2.1、在类的构造文件中绑定信号与槽
QObject::connect(sender,SIGNAL(signal()),receiver, SLOT(slot()));sender------发送信号的对象SIGNAL(signal())--------信号类型receiver-------接收者SLOT(slot())--------槽函数
2.2、类.h头文件声明槽函数
2.3、槽函数执行功能
2.4、报错解决
出现QObject::connect: Parentheses expected,这个错是我自己粗心,但也不得不吐槽下qt的不合理设计,connect时槽函数忘记加括号也可以编译通过,而且connect忘记加括号的槽函数是可以接收到信号的,因为我的槽函数里面打印的东西确实有输出。有点坑。
在类中使用信号/槽时一定要加Q_OBJECT宏,signal和slots的参数要一样
槽函数加(): connect(toTopLayerAction, SIGNAL(triggered()), this, SLOT(slotLayerTop())); // 正确
切记忘了():connect(toTopLayerAction, SIGNAL(triggered()), this, SLOT(slotLayerTop)); // 错误
3、第三种实现方法
3.1、使用Lambda表达式在构造函数中实现
QObject::connect(sender,&Sender::signal, [=]() {/* lambda body */ });
4、第四种方式
QObject::connect(sender,&Sender::signal, receiver,&Receiver::slot);
4.1、在构造函数中实现
5、自定义信号与槽
class MyClass : public QObject {Q_OBJECT
public:MyClass();
signals:void mySignal(int value);
};
class MyClass : public QObject {Q_OBJECT
public slots:void mySlot(int value);
};
MyClass *myObject = new MyClass();
connect(myObject, SIGNAL(mySignal(int)), myObject, SLOT(mySlot(int)));
emit mySignal(123);
#include "widget.h"
#include "ui_widget.h"
//widget.cpp
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//连接槽 ,触发对象和接收都是自己QObject::connect(this ,SIGNAL(mysignal()) ,this ,SLOT(myslot()));QObject::connect( this ,SIGNAL(mysignalparams(int)) ,this ,SLOT(myslotparams(int)) );emit mysignal(); //触发信号emit mysignalparams(999); //触发带参数的信号
}Widget::~Widget()
{delete ui;
}void Widget::myslot() //槽函数
{std::cout << "myslot" << std::endl;
}void Widget::myslotparams(int value)
{std::cout << "myslotparams ," << value << std::endl;qDebug() << "This is value = " << value;
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <iostream>
#include <QDebug>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();signals: //自定义信号void mysignal();void mysignalparams(int value);public slots: //自定义槽void myslot();void myslotparams(int value);private:Ui::Widget *ui;
};
#endif // WIDGET_H
执行结果
5.2、例2
创建两个类,比如Sender
和Receiver
。Sender
类将发出信号,而Receiver
类将接收并处理这个信号。
// Sender.h
#ifndef SENDER_H
#define SENDER_H #include <QObject> class Sender : public QObject { Q_OBJECT
public: explicit Sender(QObject *parent = nullptr); signals: void mySignal(); //信号public slots: void emitSignal();
}; #endif // SENDER_H
// Sender.cpp
#include "Sender.h" Sender::Sender(QObject *parent) : QObject(parent) {
} void Sender::emitSignal() { emit mySignal(); //发送信号
}
// Receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H #include <QObject>
#include <QDebug> class Receiver : public QObject { Q_OBJECT
public: explicit Receiver(QObject *parent = nullptr); public slots: void handleSignal(); //槽函数
}; #endif // RECEIVER_H
// Receiver.cpp
#include "Receiver.h" Receiver::Receiver(QObject *parent) : QObject(parent) {
} void Receiver::handleSignal() { qDebug() << "Signal received!"; //槽函数
}
// main.cpp
#include <QCoreApplication>
#include "Sender.h"
#include "Receiver.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Sender sender; Receiver receiver; //连接信号与槽QObject::connect(&sender, &Sender::mySignal, &receiver, &Receiver::handleSignal); sender.emitSignal(); // 调用函数发送信号return a.exec();
}
6、QDebug()
qDebug() << "This is a debug message";
int value = 10;
qDebug() << "The value is" << value;