《Qt》part 3 Qt5.5.0窗口之间传值(一)

2023-12-20 08:18
文章标签 qt 传值 窗口 part 之间 qt5.5

本文主要是介绍《Qt》part 3 Qt5.5.0窗口之间传值(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

part 3 Qt5.5.0窗口之间传值(一)

一、信号与槽机制传值

1、主窗口与子窗口之间传值:

主窗口为ParentChild,继承MainWindow类

子窗口为TestDialog,继承QDialog类

1)TestDialog:


(1)testdialog.h

#ifndef TESTDIALOG_H
#define TESTDIALOG_H#include <QtWidgets/QDialog>
#include "ui_testdialog.h"//namespace Ui{
//	class TestDialog;
//}class TestDialog : public QDialog
{Q_OBJECTpublic:TestDialog(QWidget *parent = 0);~TestDialog();signals:void dlgReturn(/*int*/QString);
private slots:void pushButton_clicked();
private:Ui::TestDialog ui;};#endif // TESTDIALOG_H

(2)、testdialog.cpp

#include "testdialog.h"TestDialog::TestDialog(QWidget *parent): QDialog(parent){ui.setupUi(this);connect(ui.on_pushButton_ok, SIGNAL(clicked()), this, SLOT(pushButton_clicked()));//确定按钮得信号槽函数,点击后发射信号dlgReturn(int or Qstring)
}TestDialog::~TestDialog()
{}void
TestDialog::pushButton_clicked()
{emit dlgReturn(ui.lineEdit->text()/*.toInt()*/);//close();}
2)ParentChild:

(1)parentchild.h

#ifndef PARENTCHILD_H
#define PARENTCHILD_H#include <QtWidgets/QMainWindow>
#include "ui_parentchild.h"#include "testdialog.h"
#include "ui_testdialog.h"#include <qlabel.h>
#include <qstring.h>namespace Ui{class ParentChild;
}
class ParentChild : public QMainWindow
{Q_OBJECTpublic:ParentChild(QWidget *parent = 0);~ParentChild();private slots:
void showValue(QString/*int*/);
public slots:void on_pushButton_clicked();private:Ui::ParentChildClass ui;};#endif // PARENTCHILD_H

(2)parentchild.cpp

#include "parentchild.h"
#include "ui_parentchild.h"#include "testdialog.h"ParentChild::ParentChild(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);this->setWindowTitle(QString::fromLocal8Bit("窗口传值测试"));//关联信号和槽函数connect(ui.pushButton_Dlg, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));//主窗口信号槽函数,点击后弹出对话框,连接子窗口的发射信号,同<span style="white-space:pre">											</span>  //时触发显示函数showValue(int or Qstring)
}ParentChild::~ParentChild()
{}void
ParentChild::showValue(/*int*/QString val)
{QString str = QString("%1").arg(val);ui.label->setText(str);
}void 
ParentChild::on_pushButton_clicked()
{TestDialog *dlg = new TestDialog(this);dlg->setModal(false);//非模态为false,模态为trueconnect(dlg, SIGNAL(dlgReturn(QString/*int*/)), this, SLOT(showValue(QString/*int*/)));//连接信号与槽dlg->show();
}
3)main.cpp

这个不用做任何更改

#include "parentchild.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);ParentChild w;w.show();return a.exec();
}

运行:



二、Public函数传值

1、子窗口向主窗口传值

主窗口为ParentChild,继承MainWindow类

子窗口为TestDialog,继承QDialog类

该程序先弹出子窗口,然后传值给主窗口
1)TestDialog子窗口
子窗口中通过按钮来触发传参

(1)testdialog.h
#ifndef TESTDIALOG_H
#define TESTDIALOG_H#include <QtWidgets/QDialog>
#include "ui_testdialog.h"class TestDialog : public QDialog
{Q_OBJECTpublic:TestDialog(QWidget *parent = 0);~TestDialog();private slots:void pushButton_clicked();private:Ui::TestDialog ui;
};#endif // TESTDIALOG_H

(2)testdialog.cpp
#include "testdialog.h"/
#include "ui_parentchild.h"
#include "parentchild.h"
/TestDialog::TestDialog(QWidget *parent): QDialog(parent)
{ui.setupUi(this);connect(ui.pushButton_ok, SIGNAL(clicked()), this, SLOT(pushButton_clicked()));
}TestDialog::~TestDialog()
{}void 
TestDialog::pushButton_clicked()
{ParentChild *w  = new ParentChild;w->dlgReturn(ui.lineEdit->text());w->show();
}

2)ParentChild主窗口
主窗口中没有信号槽触发函数

(1)parentchild.h
#ifndef PARENTCHILD_H
#define PARENTCHILD_H#include <QtWidgets/QMainWindow>
#include "ui_parentchild.h"#include "testdialog.h"
#include "ui_testdialog.h"
class ParentChild : public QMainWindow
{Q_OBJECTpublic:ParentChild(QWidget *parent = 0);~ParentChild();public:void dlgReturn(QString data);private:Ui::ParentChild ui;
};#endif // PARENTCHILD_H

(2)parentchild.cpp
#include "parentchild.h"ParentChild::ParentChild(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);}ParentChild::~ParentChild()
{}void 
ParentChild::dlgReturn(QString data)
{ui.label->setText(data);ui.lineEdit_1->setText(data);
}
3)main.cpp
这个main.cpp中的代码是首先弹出子窗口来传值的注意下,头文件中要加入子窗口头文件,主窗口的头文件可以注释也可以保留
/
//#include "parentchild.h"
#include "testdialog.h"
/
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);//ParentChild w;TestDialog w;w.show();return a.exec();
}

运行:





这篇关于《Qt》part 3 Qt5.5.0窗口之间传值(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/515345

相关文章

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自