Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report