Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Cross-window signal slot Communication in Qt Development

Shulou Source: shulou.com Published: 2022-06-02 04:10:21 11月04日 Update

This article mainly introduces "Qt development how to achieve cross-window signal slot communication", in daily operation, I believe many people in Qt development how to achieve cross-window signal slot communication problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Qt development how to achieve cross-window signal slot communication" doubts helpful! Next, please follow the small series to learn together!

Multi-window communication, if the window class objects contain each other, you can directly open the public interface call, however, in many cases between the main window and child window to achieve asynchronous message communication, you must rely on cross-window signal slot, the following is a simple example.

parent window

mainwindow.h

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include #include #include class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ~MainWindow();private slots: void receiveMsg(QString str);private: QLabel *label;};#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"#include "subwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ setWindowTitle("MainWindow"); setFixedSize(400, 300); // add text label label = new QLabel(this); label->setText("to be changed"); // open sub window and connect SubWindow *subwindow = new SubWindow(this); connect(subwindow, SIGNAL(sendText(QString)), this, SLOT(receiveMsg(QString))); subwindow->show(); // use open or exec both ok}void MainWindow::receiveMsg(QString str){ // receive msg in the slot label->setText(str);}MainWindow::~MainWindow(){} child window

subwindow.h

#ifndef SUBWINDOW_H#define SUBWINDOW_H#include class SubWindow : public QDialog{ Q_OBJECTpublic: explicit SubWindow(QWidget *parent = 0);signals: void sendText(QString str);public slots: void onBtnClick();};#endif // SUBWINDOW_H

subwindow.cpp

#include "QPushButton"#include "subwindow.h"SubWindow::SubWindow(QWidget *parent) : QDialog(parent){ setWindowTitle("SubWindow"); setFixedSize(200, 100); QPushButton *button = new QPushButton("click", this); connect(button, SIGNAL(clicked()), this, SLOT(onBtnClick()));}void SubWindow::onBtnClick(){ // send signal emit sendText("hello qt");} At this point, the study of "Qt development how to achieve cross-window signal slot communication" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Communication signal development learning between more help utility next object situation interface article method message theory knowledge example article website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information macOS MariaDB Shulou Tech Info