網(wǎng)站首頁 編程語言 正文
多窗口通信,如果是窗口類對象之間互相包含,則可以直接開放public接口調(diào)用,不過,很多情況下主窗口和子窗口之間要做到異步消息通信,就必須依賴到跨窗口的信號槽,以下是一個簡單的示例。
母窗口
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include <QString> class MainWindow : public QMainWindow { ? ? Q_OBJECT public: ? ? 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() { }
子窗口
subwindow.h
#ifndef SUBWINDOW_H #define SUBWINDOW_H #include <QDialog> class SubWindow : public QDialog { ? ? Q_OBJECT public: ? ? 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"); }
截圖:
基本思路:
1、子窗口發(fā)送信號
2、主窗口打開子窗口,并創(chuàng)建好信號槽關(guān)聯(lián)
3、通過信號槽函數(shù)傳遞消息參數(shù)
原文鏈接:https://tashaxing.blog.csdn.net/article/details/83387269
相關(guān)推薦
- 2022-05-04 詳解python的異常捕獲_python
- 2022-12-23 C++中類的成員函數(shù)及內(nèi)聯(lián)函數(shù)使用及說明_C 語言
- 2022-03-19 關(guān)于docker中?WSL?配置與修改問題_docker
- 2022-11-24 Django?ORM?F對象和Q對象查詢_python
- 2022-04-04 Python數(shù)據(jù)處理-導(dǎo)入導(dǎo)出excel數(shù)據(jù)_python
- 2022-01-13 使用postcss插件配置rem和手寫rem的方法
- 2022-04-28 C/C++的關(guān)鍵字之static你了解嗎_C 語言
- 2023-01-17 C++使用宏實(shí)現(xiàn)動態(tài)庫加載_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支