多窗口通信,如果是窗口类对象之间互相包含,则可以直接开放public接口调用,不过,很多情况下主窗口和子窗口之间要做到异步消息通信,就必须依赖到跨窗口的信号槽,以下是一个简单的示例。
mainwindow.h
#ifndef mainwindow_h#define mainwindow_h#include <qmainwindow>#include <qlabel>#include <qstring>class mainwindow : 员工评价public qmainwindow{ q_objectpublic: 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){ twindowtitle("mainwindow"); tfixedsize(400, 300); // add text label label = new qlabel(this); label->ttext("to be changed"); // open sub window and connect subwindow *subwindow = new subwindow(this); connect(subwindow, signal(ndtext(qstring)), this, slot(receivemsg(qstring))); subwindow->show(); // u open or exec both ok}void mainwindow::receivemsg(qstring str){ // receive msg in the slot label->ttext(str);}mainwindow::~mainwindow(){}
subwindow.h
#ifndef subwindow小猴子下山的故事_h#define subwindow_h#include <qdialog>class subwindow : public qdialog{ q_objectpublic: explicit subwindow(qwidget *parent = 0);signals: void ndtext(qstring str);public slots: void onbtnclick();};#endif // subwindow_h
subwindow.cpp
#include "qpushbutton"#include "subwindow.h"subwindow::subwindow(qwidget *parent) : qdialog(parent){ twindowtitle("subwindow"); tfixedsize(200, 100); qpushbutton *button = new qpushbutton("click", this); connect(button, signal(clicked()), this, slot(onbtnclick()));}void subwindow::onbtnclick(){ // s默哀三分钟end signal emit ndtext("hello qt");}
截图:
基本思路:
1、子窗口发送信号
2、主窗口打开子窗口,大树下并创建好信号槽关联
3、通过信号槽函数传递消息参数
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 10:16:26,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/df22375315e0a4b1c93c584aa22435da.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Qt开发实现跨窗口信号槽通信.doc
本文 PDF 下载地址:Qt开发实现跨窗口信号槽通信.pdf
留言与评论(共有 0 条评论) |