Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信
本文實(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ā)出者,指針類(lèi)型 ? ? ? ? * &QPushButton::pressed:處理的信號(hào), ?&發(fā)送者的類(lèi)名::信號(hào)名字 ? ? ? ? * this: 信號(hào)接收者 ? ? ? ? * &MainWidget::close: 槽函數(shù),信號(hào)處理函數(shù) ?&接收的類(lèi)名::槽函數(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,"唐維康"); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Qt串口通信開(kāi)發(fā)之QSerialPort模塊詳細(xì)使用方法與實(shí)例
- Qt串口通信開(kāi)發(fā)之Qt串口通信模塊QSerialPort開(kāi)發(fā)完整實(shí)例(串口助手開(kāi)發(fā))
- PyQt5通信機(jī)制 信號(hào)與槽詳解
- Qt串口通信開(kāi)發(fā)之QSerialPort模塊Qt串口通信接收數(shù)據(jù)不完整的解決方法
- QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
- Qt串口通信開(kāi)發(fā)之QSerialPort模塊簡(jiǎn)單使用方法與實(shí)例
- QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)
- QT串口通信的實(shí)現(xiàn)方法
- QT5實(shí)現(xiàn)UDP通信的示例代碼
- Qt實(shí)現(xiàn)簡(jiǎn)單UDP通信
相關(guān)文章
利用C++實(shí)現(xiàn)從std::string類(lèi)型到bool型的轉(zhuǎn)換
利用C++實(shí)現(xiàn)從std::string類(lèi)型到bool型的轉(zhuǎn)換。需要的朋友可以過(guò)來(lái)參考下。希望對(duì)大家有所幫助2013-10-10C++詳解使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位
這篇文章主要介紹了C++使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C語(yǔ)言深入淺出講解順序表的實(shí)現(xiàn)
線性表是最簡(jiǎn)單的數(shù)據(jù)結(jié)構(gòu),而順序表又是最簡(jiǎn)單的線性表,其基本思想是用一段地址連續(xù)的儲(chǔ)存單元依次存儲(chǔ)線性表的數(shù)據(jù)元素,比如我們常用的一維數(shù)組,下面代碼實(shí)現(xiàn)了順序表的定義以及基本操作2022-04-04C語(yǔ)言#define拼接宏定義實(shí)現(xiàn)方式
今天小編就為大家分享一篇C語(yǔ)言#define拼接宏定義實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12c++創(chuàng)建二維動(dòng)態(tài)數(shù)組與內(nèi)存釋放問(wèn)題
這篇文章主要介紹了c++創(chuàng)建二維動(dòng)態(tài)數(shù)組與內(nèi)存釋放問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06C語(yǔ)言順序表的基本結(jié)構(gòu)與實(shí)現(xiàn)思路詳解
順序表是用一段物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。本文將通過(guò)示例為大家講解一下順序表的基本操作,需要的可以參考一下2023-02-02