網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信的具體代碼,供大家參考,具體內(nèi)容如下
知識(shí)點(diǎn)
Qt兩個(gè)窗口的建立、窗口的通信、處理子窗口的信號(hào)、信號(hào)的重載、Lamber表達(dá)式、自定義信號(hào)和自定義槽函數(shù)
結(jié)果演示
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;//關(guān)閉按鈕 ? ? 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: ? ? /* 信號(hào)必須有signals關(guān)鍵字來(lái)聲明 ? ? ? ? ? * 信號(hào)沒(méi)有返回值,但可以有參數(shù) ? ? ? ? ? * 信號(hào)就是函數(shù)的聲明,只需聲明,無(wú)需定義 ? ? ? ? ? * 使用:emit mySignal(); ? ? ? ? ? * 信號(hào)可以重載 ? ? ? */ ? ? 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("關(guān)閉"); ? ? shutdown->move(100,0); ? ? //關(guān)閉所有窗口 ? ? connect(shutdown,&QPushButton::released,this,&MainWindow::ShutDown); ? ? //主窗口隱藏,子窗口顯示 ? ? connect(p1,&QPushButton::released,this,&MainWindow::changeWin); ? ? /* &p1: 信號(hào)發(fā)出者,指針類型 ? ? ? ? * &QPushButton::pressed:處理的信號(hào), ?&發(fā)送者的類名::信號(hào)名字 ? ? ? ? * this: 信號(hào)接收者 ? ? ? ? * &MainWidget::close: 槽函數(shù),信號(hào)處理函數(shù) ?&接收的類名::槽函數(shù)名字 ? ? ? ?*/ ? ? ? ?/* 自定義槽,普通函數(shù)的用法 ? ? ? ? * Qt5:任意的成員函數(shù),普通全局函數(shù),靜態(tài)函數(shù) ? ? ? ? * 槽函數(shù)需要和信號(hào)一致(參數(shù),返回值) ? ? ? ? * 由于信號(hào)都是沒(méi)有返回值,所以,槽函數(shù)一定沒(méi)有返回值 ? ? ? ? */ ? ? //主窗口調(diào)用子窗口的信號(hào) ? ? //主窗口顯示,子窗口隱藏 ? ? //connect(&subW,&SubWidget::mySignal,this,&MainWindow::delSub);//函數(shù)重載用不了 ? ? //信號(hào)的重載 一個(gè)有參數(shù) 一個(gè)無(wú)參數(shù) ? ? //接收子窗口的信號(hào)方式一 // ? ?void (SubWidget::*SignalOnly)()=&SubWidget::mySignal; // ? ?connect(&subW,SignalOnly,this,&MainWindow::delSub); // ? ?void (SubWidget::*SignalCanShu)(int,QString)=&SubWidget::mySignal; // ? ?connect(&subW,SignalCanShu,this,&MainWindow::delSubPrint); ? ? //接收子窗口的信號(hào)方式二:宏函數(shù)SIGNAL ? ? connect(&subW,SIGNAL(mySignal()),this,SLOT(delSub())); ? ? connect(&subW,SIGNAL(mySignal(int,QString)),this,SLOT(delSubPrint(int,QString))); ? ? //Lambda表達(dá)式, 匿名函數(shù)對(duì)象 ? ? //C++11增加的新特性, 項(xiàng)目文件: CONFIG += C++11 ? ? //Qt配合信號(hào)一起使用,非常方便 ? ? QPushButton *LamPush=new QPushButton(this); ? ? LamPush->setText("Lamber表達(dá)式"); ? ? LamPush->move(180,0); ? ? int a=10; ? ? connect(LamPush,&QPushButton::clicked, ? ? ? ? ? ? [=](){ ? ? ? ? ? ? ? ? ? ? qDebug()<<a; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ); } void MainWindow::changeWin(){ ? ? this->hide(); ? ? subW.show(); } //關(guān)閉窗口 void MainWindow::ShutDown(){ ? ? this->close(); ? ? subW.close(); } void MainWindow::delSub(){ ? ? subW.hide(); ? ? this->show(); } void MainWindow::delSubPrint(int age,QString name){ ? ? // str.toUtf8() -> 字節(jié)數(shù)組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); ? ? //子窗口發(fā)送信號(hào) 信號(hào)是有參數(shù)+無(wú)參數(shù)的 ? ? connect(subP1,&QPushButton::clicked,this,&SubWidget::sendSlot); } //回調(diào)函數(shù) void SubWidget::sendSlot(){ ? ? emit mySignal(); ? ? emit mySignal(26,"唐維康"); }
原文鏈接:https://blog.csdn.net/taw19960426/article/details/121939735
相關(guān)推薦
- 2023-02-27 C語(yǔ)言中互斥鎖與自旋鎖及原子操作使用淺析_C 語(yǔ)言
- 2022-06-07 python數(shù)組的復(fù)制與列表中的pop_python
- 2022-12-13 C#實(shí)現(xiàn)字符串進(jìn)制轉(zhuǎn)換方法匯總_C#教程
- 2022-07-12 手把手教你用Redis?實(shí)現(xiàn)點(diǎn)贊功能并且與數(shù)據(jù)庫(kù)同步_Redis
- 2022-07-15 SQL?Server中執(zhí)行動(dòng)態(tài)SQL_MsSql
- 2022-09-17 Python?pandas?重命名索引和列名稱的實(shí)現(xiàn)_python
- 2023-12-13 記錄choice函數(shù)用法
- 2023-05-07 C++中set/multiset與map/multimap的使用詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤: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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支