網站首頁 編程語言 正文
多窗口通信,如果是窗口類對象之間互相包含,則可以直接開放public接口調用,不過,很多情況下主窗口和子窗口之間要做到異步消息通信,就必須依賴到跨窗口的信號槽,以下是一個簡單的示例。
母窗口
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、子窗口發送信號
2、主窗口打開子窗口,并創建好信號槽關聯
3、通過信號槽函數傳遞消息參數
原文鏈接:https://tashaxing.blog.csdn.net/article/details/83387269
相關推薦
- 2023-07-04 SpringBoot不在使用@Validated 做參數校驗但是不想在Controller層怎么辦?
- 2023-01-13 C語言實現繪制貝塞爾曲線的函數_C 語言
- 2023-02-17 python連接讀寫操作redis的完整代碼實例_python
- 2022-09-27 阿里云官方Redis開發規范總結_Redis
- 2022-03-03 設置setInterval定時器、postMessage、addEventListener監聽器
- 2022-08-15 Python包裝異常處理方法_python
- 2023-07-31 elementui使用el-upload組件實現自定義上傳
- 2022-06-06 Postgresql split_part()函數,根據符號切割字符串
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支