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 signal communication between two independent windows by Qt

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how Qt realizes the signal communication between two independent windows. I hope you will get something after reading this article. Let's discuss it together.

The details are as follows

Knowledge point

Establishment of Qt two windows, window communication, processing of sub-window signals, signal overloading, Lamber expressions, custom signals and custom slot functions

Result demonstration

Main.cpp

# include "mainwindow.h" # include "subwidget.h" # include int main (int argc, char * argv []) {QApplication a (argc, argv); MainWindow w; w.show (); return a.exec ();}

Mainwindow.h

# ifndef MAINWINDOW_H#define MAINWINDOW_H#include # include # include "subwidget.h" class MainWindow: public QMainWindow {Q_OBJECTpublic: MainWindow (QWidget * parent = 0); ~ MainWindow (); public slots: void changeWin (); void ShutDown (); void delSub (); void delSubPrint (int,QString); private: QPushButton * p1; QPushButton * shutdown;// close button SubWidget subW;}; # endif / / MAINWINDOW_H

Subwidget.h

# ifndef SUBWIDGET_H#define SUBWIDGET_H#include # include class SubWidget: public QWidget {Q_OBJECTpublic: explicit SubWidget (QWidget * parent = 0); void sendSlot (); signals: / * the signal must have the signals keyword to declare * the signal does not return a value, but it can have parameters * the signal is the declaration of the function, just declare, no need to define * use: emit mySignal () * signal can be overloaded * / void mySignal (); void mySignal (int,QString); public slots:private: QPushButton * subP1;// child window button}; # endif / / SUBWIDGET_H

Mainwindow.cpp

# include "mainwindow.h" # include / / print MainWindow::MainWindow (QWidget * parent): QMainWindow (parent) {resize (400300); this- > setWindowTitle ("BOSS"); p1=new QPushButton (this); / / p1-> setParent (this); p1-> setText ("switch to child window"); shutdown=new QPushButton (this); shutdown- > setText ("off"); shutdown- > move (100th) / / close all windows connect (shutdown,&QPushButton::released,this,&MainWindow::ShutDown); / / the main window is hidden, and the child window shows connect (p1heroines QPushButtonGrane released Magi Magi MainWindowRangchangeWin) / * & p1: signal sender, pointer type * & QPushButton::pressed: signal processed, & sender's class name:: signal name * this: signal receiver * & MainWidget::close: slot function, signal processing function & received class name:: slot function name * / / * Custom slot Usage of ordinary function * Qt5: any member function, ordinary global function, static function * slot function needs to be consistent with the signal (parameter, return value) * since the signal has no return value, the slot function must have no return value * / / the main window invokes the signal of the child window / / the main window display Child window hides / / connect (& subW,&SubWidget::mySignal,this,&MainWindow::delSub) / / function overloading cannot use / / signal overloading A signal mode with parameters and a non-parameter / / receiving child window-/ / void (SubWidget::*SignalOnly) () = & SubWidget::mySignal;// connect (& subW,SignalOnly,this,&MainWindow::delSub); / / void (SubWidget::*SignalCanShu) (int,QString) = & SubWidget::mySignal;// connect (& subW,SignalCanShu,this,&MainWindow::delSubPrint) / / signal mode 2 of receiving sub-window: macro function SIGNAL connect (& subW,SIGNAL (mySignal ()), this,SLOT (delSub (); connect (& subW,SIGNAL (mySignal (int,QString)), this,SLOT (delSubPrint (int,QString) / / Lambda expression, a new feature added to anonymous function object / / clocked expressions 11. The project file: CONFIG + = Cleavage 11 / / Qt is used with signals, which is very convenient for QPushButton * LamPush=new QPushButton (this); LamPush- > setText ("Lamber expression"); LamPush- > move (180J 0); int axiom 10 Connect (LamPush,&QPushButton::clicked, [=] () {qDebug () close (); subW.close ();} void MainWindow::delSub () {subW.hide (); this- > show ();} void MainWindow::delSubPrint (int age,QString name) {/ / str.toUtf8 ()-> byte array QByteArray / /... Data ()-> QByteArray-> char * qDebug () setText ("switch to main window"); resize (500400); / / connect (subP1,&QPushButton::clicked,this,&SubWidget::sendSlot) with parameters + no parameters in child windows;} / callback function void SubWidget::sendSlot () {emit mySignal (); emit mySignal (26, "Tang Weikang") } after reading this article, I believe you have a certain understanding of "how to achieve signal communication between two independent windows in Qt". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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