QT 信号槽/emit/两个窗口之间数据传递 您所在的位置:网站首页 信息为什么不能直接传递信息 QT 信号槽/emit/两个窗口之间数据传递

QT 信号槽/emit/两个窗口之间数据传递

2024-07-01 03:08| 来源: 网络整理| 查看: 265

QT信号槽,在两个窗口之间利用emit进行数据传输 信号槽理解 QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection) //connect通常具有5个参数,但是第五个参数一般不用,并且connect的用法形式较多 //sender:信号的发送者 //signal:发送者的函数(信号) //receiver:信号的接受者 //method:信号接受者的函数(槽函数) //例如: connect(ui->pushButton,&QPushButton::released,this,&MainWindow::ShowSubDialog); //这一行代码即可完成点击pushbutton按钮调用当前窗口的ShowSubDialog函数

但是上面所述的这种connect方法在进行有参数的信号槽传递的时候就不方便,也可以用下面这一种

connect(&SD,SIGNAL(SendData(QString)),this,SLOT(GetData(QString))); 介绍如何进行数据的传递

1.首先清楚一个逻辑,主窗口在调用子窗口时是很容易的,可以直接include“子窗体头文件”,然后定义一个私有变量即可对这个变量进行操作,所以主窗口给子窗口赋值就不赘述了。 2.但是子窗口的头文件就不要include主窗口了,可以这样做 在子窗体头文件中定义一个信号

signals: void SendData(QString str);

这里我对一个pushbutton与调用我这个发射的函数connect起来,之后在一个函数中对这个信号进行发射,即emit 一下

connect(ui->pushButton,&QPushButton::released,this,&SubDialog::SendSlot); void SubDialog::SendSlot() { emit SendData(ui->lineEdit->text()); }

之后在主函数中将子窗口对象、信号等connect一下

connect(&SD,SIGNAL(SendData(QString)),this,SLOT(GetData(QString))); void MainWindow::GetData(QString str) { ui->label->setText(str); this->show(); SD.hide(); }

这样既可完成两个窗口之间的数据往来 3.下面总计一下总的一个逻辑,其核心是在子窗口中定义信号函数,在任意一个子窗口函数中emit信号函数,同时在主窗口函数中对子窗口对象、信号函数、与主窗口的函数做connect

在这里插入图片描述在这里插入图片描述在这里插入图片描述

下面是全部代码 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include "subdialog.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void ShowSubDialog(); void GetData(QString str); signals: private: Ui::MainWindow *ui; SubDialog SD; }; #endif // MAINWINDOW_H #ifndef SUBDIALOG_H #define SUBDIALOG_H #include namespace Ui { class SubDialog; } class SubDialog : public QDialog { Q_OBJECT public: explicit SubDialog(QWidget *parent = 0); ~SubDialog(); private: Ui::SubDialog *ui; private slots: void SendSlot(); signals: void SendData(QString str); }; #endif // SUBDIALOG_H #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton,&QPushButton::released,this,&MainWindow::ShowSubDialog); connect(&SD,SIGNAL(SendData(QString)),this,SLOT(GetData(QString))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::ShowSubDialog() { SD.show(); this->hide(); } void MainWindow::GetData(QString str) { ui->label->setText(str); this->show(); SD.hide(); } #include "subdialog.h" #include "ui_subdialog.h" SubDialog::SubDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SubDialog) { ui->setupUi(this); connect(ui->pushButton,&QPushButton::released,this,&SubDialog::SendSlot); } SubDialog::~SubDialog() { delete ui; } void SubDialog::SendSlot() { emit SendData(ui->lineEdit->text()); } #include "mainwindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

第一次写博客,本人也还是个小白,敬请大佬指正~~~



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有