当前位置:网站首页>Qt-2-signal and slot
Qt-2-signal and slot
2022-06-21 13:59:00 【Do not deceive the dark room__】
Signal and slot
Signals and slots are Qt stay C++ New features based on , Be similar to Java、C# Callback mechanism , But it is more convenient to use , It is easy to make passive function calls between different components , It's a communication mechanism .
A signal is a function
Slots are also a function
To use signal functions and slot functions , There are two prerequisites :
The object of communication must be from QObject derived
Class must be added O_OBJECT macro
The function prototype
QObject::connect(const QObject * sender,
const char * signal,
const QObject * receiver,
const char * method) [static]
Parameter one : Launcher , Is the calling object of the signal function
Parameter two : Signal function , It is a function automatically triggered by the system under certain conditions , Put it on the outside SIGNAL()
Parameter 3 : The receiver , Is the object that executes the slot function .
Parameter 4 : Slot function , A function to be executed when a signal is transmitted , Put it on the outside SLOT()
The receiver binds the sender's signal , Once the transmitter sends this signal , The receiver automatically executes the slot function .
The signal slot can be connected , You can also disconnect , After disconnection, the previously connected signal slot will not take effect . There are two common ways to disconnect .
Automatic disconnection
When the sender or receiver object is destroyed , The signal slot will also be automatically disconnected .
Manual disconnection
have access to disconnect Function to disconnect the previously connected signal slot , Its parameters and connect equally .
Three binding methods
The following is an explanation of three signal slot binding methods .
1. Own signal → With groove
Regardless of the signal function , Or slot function , You don't need to write it yourself , Just look up the documentation and find out .
【 Example 】 Click button , close window .
Launcher : Button object
Signal function :clicked
The receiver : Window object
Slot function :close
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *btn;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
btn = new QPushButton(" close ",this);
btn->move(200,200);
// Click button , close window
connect(btn,SIGNAL(clicked()),this,SLOT(close()));
// disconnect
// disconnect(btn,SIGNAL(clicked()),this,SLOT(close()));
}
Dialog::~Dialog()
{
delete btn;
}
2. Own signal → Custom slot function
The signal function needs to consult the documentation to find out , Slot functions need to be written by yourself .
Slot function is a special member function , Just add... To the declaration slots Key words can be used .
【 Example 】 Click button , Move the window and print out its current coordinates .
Launcher : Button object
Signal function :clicked
The receiver : Custom window class object
Slot function : Custom slot function
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QDebug>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *btn;
// Private slot function declaration
private slots:
void mySlot();
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
resize(200,200);
btn = new QPushButton(" Trigger ",this);
btn->move(100,100);
connect(btn,SIGNAL(clicked()),this,SLOT(mySlot()));
}
void Dialog::mySlot()
{
// Get the current coordinates first
int x = this->x();
int y = this->y();
// Lower right
x += 10;
y += 10;
// Move
move(x,y);
qDebug() << x << "," << y;
}
Dialog::~Dialog()
{
delete btn;
}
3. Custom signal → Custom slot function / With slot function
The signal function needs to be written by yourself , Slot functions can be written and viewed by themselves .
Be careful , Signal functions are not authorized , And only the declaration has no definition .
【 Example 】 Click button , Trigger custom slot function 1, In the custom slot function 1 Transmit custom signal in ; Use a custom signal to trigger a custom slot function 2.
Click button → Slot function 1( Sending custom signals )
Custom signal → Slot function 2
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QDebug>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *btn;
private slots:
// Slot function 1
void mySlot1();
void mySlot2();
// Custom signal function
signals:
void mySignal();
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
btn = new QPushButton(" Sending custom signals ",this);
btn->move(200,200);
// Click button , Sending custom signals
connect(btn,SIGNAL(clicked()),this,SLOT(mySlot1()));
connect(this,SIGNAL(mySignal()),this,SLOT(mySlot2()));
}
void Dialog::mySlot1()
{
// Sending custom signals
emit mySignal();
}
void Dialog::mySlot2()
{
qDebug() << " Received custom signal !";
}
Dialog::~Dialog()
{
delete btn;
}
Signal slot transmission parameters
In addition to the basic communication function, the signal slot , You can also carry data for transmission .
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *btn;
private slots:
// Custom slot function 1
void mySlot1();
// Custom slot function 2: receive int Parameters
void mySlot2(int);
signals:
// Parameters are carried data , Can be emitted to the slot function
void mySignal(int);
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
resize(400,400);
btn = new QPushButton("0",this);
connect(btn,SIGNAL(clicked()),this,SLOT(mySlot1()));
connect(this,SIGNAL(mySignal(int)),this,SLOT(mySlot2(int)));
}
void Dialog::mySlot1()
{
// Count
static int count = 0;
count++;
// Send out the number of times through the signal function
emit mySignal(count);
}
void Dialog::mySlot2(int count)
{
// QString QString::number(int n, int base = 10) [static]
QString text = QString::number(count);
// Display the times on the window title
setWindowTitle(text);
}
Dialog::~Dialog()
{
delete btn;
}
matters needing attention :
1. You can pass any number of parameters
2. Parameter type must match
3. The number of parameters of the signal can be greater than or equal to the number of parameters of the slot function
If the above code is written according to the normal development method , It can be optimized to :
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *btn;
int count = 0;
private slots:
void mySlot();
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
resize(300,300);
btn = new QPushButton("0",this);
connect(btn,SIGNAL(clicked()),this,SLOT(mySlot()));
}
void Dialog::mySlot()
{
setWindowTitle(QString::number(++count));
}
Dialog::~Dialog()
{
delete btn;
}
边栏推荐
- 7hutool actual fileutil file tool class (common operation methods for more than 100 files)
- 1. memory partition model
- Automation operation and maintenance 1 - installation and deployment of ansible
- Declare war on uncivilized code II
- How to guarantee the test coverage
- 6. functions
- PostgreSQL query by date range
- Design interface automation test cases by hand: establish database instances and test case tables
- 哪个期货平台 交易更安全放心。求推荐。
- C language elementary (VII) structure
猜你喜欢

Teach you how to design interface automation test cases: extract interface information and analyze it

Navigation bar switching, message board, text box losing focus

CSDN is the only one: detailed tutorial teaching on how to connect multiple mobile phones by appium+pytest+allure+jenkins

17 commonly used o & M monitoring systems

2021 the latest selenium truly bypasses webdriver detection
![[graduation project recommendation] - personnel management system](/img/b5/4235b54fa0da9e4637010a68bf848b.jpg)
[graduation project recommendation] - personnel management system

MySQL - table join and join

MySQL - data type

Alibaba cloud log service is available in Net project

Iterm2 file transfer with remote server
随机推荐
Redis learning (1) -- overview and common commands
哪個期貨平臺 交易更安全放心。求推薦。
Heat mapping using Seaborn
What is Devops in an article?
[Goo Goo donkey takeout rebate system] customer service configuration tutorial of takeout CPS project - (attached with picture and text building tutorial)
Setting of Seaborn drawing style
MySQL - view properties
Highly available configuration of database (MySQL)
Kube-prometheus grafana安装插件和grafana-image-renderer
Babbitt yuancosmos daily must read: wechat may ban a official account for the first time on the grounds of "involving secondary transactions in digital collections", and the new regulations of the pla
seaborn数据总体分布的可视化策略
Which futures trading platform is more secure. Ask for recommendation.
Iterm2 file transfer with remote server
3. operator
Prepare for the golden three silver four, are you ready? Summary of software test interview questions
[graduation project recommendation] - personnel management system
MySQL - adding, deleting, querying and modifying tables
Never change
seaborn绘图风格的设置
Lamp architecture 5 - MySQL Cluster and master-slave structure