当前位置:网站首页>QT custom class uses custom parametric signals and slots
QT custom class uses custom parametric signals and slots
2022-07-24 03:14:00 【Listen to MISA】
Recently, signals have been used in custom classes to connect to MainWindow Slot function in class , The specific operation is as follows :
Key points : Inherit QObject
Initialize the custom class in the header file MyThread1, Here due to inherited QThread Has inherited QObject, No nested inheritance , Therefore, it is unnecessary public QObject, But still need to join Q_OBJECT To guarantee moc The compiler can connect signals and slot functions 
// Location :mainwindow.h
// An instance of a thread class , If you only use custom signals, in addition to signals Don't care about other parts
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;
};
Main points two : Custom signals and slots
Declare signals in custom classes pos_update(QString x,QString y), But it's just a signal , No need to cpp The implementation is given in the document 
stay MainWindow Add the slot function corresponding to the signal 
Be careful : Number of parameters of signal and slot 、 The type and order should be consistent ( There can be more signal parameters than slot functions , But it's not recommended either , On the contrary, it is absolutely impossible to have more slot function parameters than signal parameters ) As for using user-defined data types as signal parameters, you can refer to link
The slot function must be implemented :
// Location :mainwindow.cpp
// After receiving the message from another thread, display it in ui On the interface
void MainWindow::update(QString x,QString y)
{
ui->pos_x->setText(x);
ui->pos_y->setText(y);
}
Main points three : Connect the signal to the slot
It is best to use pointers when instantiating classes , Convenient for signal connection , In the figure below, there are two ways to express signal connection , It's all... In essence connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method,
Qt::ConnectionType type = Qt::AutoConnection)
See [ link ](https://blog.csdn.net/qq_27898413/article/details/11967
7345)
// Location :mainwindow.cpp Declared global variables
MyThread1 *thread1 = new MyThread1();
// Location :mainwindow.cpp in MainWinw Class
connect(thread1,&MyThread1::pos_update,this,&MainWindow::update);
Point four : Sending signal
Directly where the signal needs to be sent emit that will do
// Location :mainwindow.cpp Any need to call update The place of
emit pos_update(posx,posy);
边栏推荐
猜你喜欢

The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)

In the future, when the interviewer asks why you don't recommend using select *, please answer him loudly!

MySQL sub database and sub table and its smooth expansion scheme

微信公众号在线客服接入发方法和功能详解

IDEA Clone的项目报Cannot resolve symbol ‘Override‘

O3DE 的Lumberyard 游戏引擎

Honey, we are homeless now

AcWing 4498. 指针 (DFS)

Super complete PMP reference document summary

Microsoft win11/10 package manager Winget will support the installation of applications from zip files
随机推荐
Super complete PMP reference document summary
Keras deep learning practice (15) -- realize Yolo target detection from scratch
uva1445
FTP service and configuration
FTP服务与配置
WPS前骨干历时10年打造新型软件,Excel用户:我为此改用WPS
Basic use of Pinia
Ugui source code analysis - stencilmaterial
Summernote font displays Chinese
In the future, when the interviewer asks why you don't recommend using select *, please answer him loudly!
CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID
kettle
OSPF routing control
Lumberyard game engine of o3de
Qt自定义类使用自定义含参信号与槽
B. Eastern Exhibition- Codeforces Round #703 (Div. 2)
C文件操作详解
C language exercises
IDEA Clone的项目报Cannot resolve symbol ‘Override‘
C dynamic memory management details