網站首頁 編程語言 正文
本文實例為大家分享了Qt實現兩個獨立窗口的信號通信的具體代碼,供大家參考,具體內容如下
知識點
Qt兩個窗口的建立、窗口的通信、處理子窗口的信號、信號的重載、Lamber表達式、自定義信號和自定義槽函數
結果演示
main.cpp
#include "mainwindow.h" #include "subwidget.h" #include <QApplication> 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 <QMainWindow> #include <QPushButton> #include "subwidget.h" class MainWindow : public QMainWindow { ? ? Q_OBJECT public: ? ? MainWindow(QWidget *parent = 0); ? ? ~MainWindow(); public slots: ? ? void changeWin(); ? ? void ShutDown(); ? ? void delSub(); ? ? void delSubPrint(int,QString); private: ? ? QPushButton *p1; ? ? QPushButton *shutdown;//關閉按鈕 ? ? SubWidget subW; }; #endif // MAINWINDOW_H
subwidget.h
#ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QWidget> #include <QPushButton.h> class SubWidget : public QWidget { ? ? Q_OBJECT public: ? ? explicit SubWidget(QWidget *parent = 0); ? ? void sendSlot(); signals: ? ? /* 信號必須有signals關鍵字來聲明 ? ? ? ? ? * 信號沒有返回值,但可以有參數 ? ? ? ? ? * 信號就是函數的聲明,只需聲明,無需定義 ? ? ? ? ? * 使用:emit mySignal(); ? ? ? ? ? * 信號可以重載 ? ? ? */ ? ? void mySignal(); ? ? void mySignal(int,QString); public slots: private: ? ? QPushButton *subP1;//子窗口按鈕 }; #endif // SUBWIDGET_H
mainwindow.cpp
#include "mainwindow.h" #include <QDebug> //打印 MainWindow::MainWindow(QWidget *parent) ? ? : QMainWindow(parent) { ? ? resize(400,300); ? ? this->setWindowTitle("BOSS"); ? ? p1=new QPushButton(this); ? ? //p1->setParent(this); ? ? p1->setText("切換到子窗口"); ? ? shutdown=new QPushButton(this); ? ? shutdown->setText("關閉"); ? ? shutdown->move(100,0); ? ? //關閉所有窗口 ? ? connect(shutdown,&QPushButton::released,this,&MainWindow::ShutDown); ? ? //主窗口隱藏,子窗口顯示 ? ? connect(p1,&QPushButton::released,this,&MainWindow::changeWin); ? ? /* &p1: 信號發出者,指針類型 ? ? ? ? * &QPushButton::pressed:處理的信號, ?&發送者的類名::信號名字 ? ? ? ? * this: 信號接收者 ? ? ? ? * &MainWidget::close: 槽函數,信號處理函數 ?&接收的類名::槽函數名字 ? ? ? ?*/ ? ? ? ?/* 自定義槽,普通函數的用法 ? ? ? ? * Qt5:任意的成員函數,普通全局函數,靜態函數 ? ? ? ? * 槽函數需要和信號一致(參數,返回值) ? ? ? ? * 由于信號都是沒有返回值,所以,槽函數一定沒有返回值 ? ? ? ? */ ? ? //主窗口調用子窗口的信號 ? ? //主窗口顯示,子窗口隱藏 ? ? //connect(&subW,&SubWidget::mySignal,this,&MainWindow::delSub);//函數重載用不了 ? ? //信號的重載 一個有參數 一個無參數 ? ? //接收子窗口的信號方式一 // ? ?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 ? ? connect(&subW,SIGNAL(mySignal()),this,SLOT(delSub())); ? ? connect(&subW,SIGNAL(mySignal(int,QString)),this,SLOT(delSubPrint(int,QString))); ? ? //Lambda表達式, 匿名函數對象 ? ? //C++11增加的新特性, 項目文件: CONFIG += C++11 ? ? //Qt配合信號一起使用,非常方便 ? ? QPushButton *LamPush=new QPushButton(this); ? ? LamPush->setText("Lamber表達式"); ? ? LamPush->move(180,0); ? ? int a=10; ? ? connect(LamPush,&QPushButton::clicked, ? ? ? ? ? ? [=](){ ? ? ? ? ? ? ? ? ? ? qDebug()<<a; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ); } void MainWindow::changeWin(){ ? ? this->hide(); ? ? subW.show(); } //關閉窗口 void MainWindow::ShutDown(){ ? ? this->close(); ? ? subW.close(); } void MainWindow::delSub(){ ? ? subW.hide(); ? ? this->show(); } void MainWindow::delSubPrint(int age,QString name){ ? ? // str.toUtf8() -> 字節數組QByteArray ? ? ?// ……data() ?-> QByteArray -> char * ? ? qDebug() << age << name.toUtf8().data(); } MainWindow::~MainWindow() { }
subwidget.cpp
#include "subwidget.h" SubWidget::SubWidget(QWidget *parent) : ? ? QWidget(parent) { ? ? this->setWindowTitle("SUB"); ? ? subP1=new QPushButton(this); ? ? subP1->setText("切換到主窗口"); ? ? resize(500,400); ? ? //子窗口發送信號 信號是有參數+無參數的 ? ? connect(subP1,&QPushButton::clicked,this,&SubWidget::sendSlot); } //回調函數 void SubWidget::sendSlot(){ ? ? emit mySignal(); ? ? emit mySignal(26,"唐維康"); }
原文鏈接:https://blog.csdn.net/taw19960426/article/details/121939735
相關推薦
- 2023-07-14 如何限制請求的并發數量
- 2023-06-13 C語言中函數返回值不一致問題_C 語言
- 2023-11-19 樹莓派/arm設備上安裝火狐Firefox瀏覽器
- 2022-07-03 C#預定義的基礎類型轉換_C#教程
- 2023-07-29 koa2+sequelize中websocket的使用
- 2022-09-08 pandas刪除部分數據后重新生成索引的實現_python
- 2022-06-07 Android音視頻開發之MediaPlayer使用教程_Android
- 2022-03-30 Python機器學習應用之樸素貝葉斯篇_python
- 最近更新
-
- 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同步修改后的遠程分支