• qt界面之间传递数据


    前言,最近在做qt项目,登录成功后,在新的界面中展示”账号,欢迎你登录!",因此需要进行传递参数,根据本人写的调试成功的写一个随笔,直接把测试代码贴出来,展示!

     代码展示

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include "form.h"
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. Widget(QWidget *parent = nullptr);
    12. ~Widget();
    13. signals:
    14. //发送数据的信号,不需要实体函数,在头文件定义就可以
    15. void sendData(QString);
    16. private:
    17. QPushButton *Btn;
    18. Form *f;
    19. QLineEdit *qle;
    20. private slots:
    21. void clicked_button();
    22. };
    23. #endif // WIDGET_H

    widget.cpp

    1. #include "widget.h"
    2. Widget::Widget(QWidget *parent)
    3. : QWidget(parent)
    4. {
    5. this->setWindowTitle("widget发送界面!");
    6. this->setFixedSize(800,600);
    7. Btn = new QPushButton(this);
    8. Btn->setGeometry(300,200,100, 50);
    9. qle = new QLineEdit(this);
    10. qle->setGeometry(120,20,200,40);
    11. Btn->setText("发送");
    12. f = new Form();
    13. connect(Btn,SIGNAL(clicked()),this,SLOT(clicked_button()));
    14. //这里是连接信号和槽函数的重点
    15. connect(this,SIGNAL(sendData(QString)),f,SLOT(getData(QString)));
    16. }
    17. Widget::~Widget()
    18. {
    19. }
    20. void Widget::clicked_button()
    21. {
    22. f->show();
    23. QString strss = qle->text();
    24. emit sendData(strss);//发送数据,使用emit
    25. }

    form.h

    1. #ifndef FORM_H
    2. #define FORM_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. namespace Ui {
    8. class Form;
    9. }
    10. class Form : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit Form(QWidget *parent = nullptr);
    15. ~Form();
    16. private:
    17. QLineEdit *qle1;
    18. private slots:
    19. //得到数据的槽函数
    20. void getData(QString);
    21. };
    22. #endif // FORM_H

    form.cpp

    1. #include "form.h"
    2. //#include "ui_form.h"
    3. //#include "widget.h"
    4. Form::Form(QWidget *parent) :
    5. QWidget(parent)
    6. {
    7. this->setWindowTitle("form接收界面!");
    8. this->setFixedSize(600,300);
    9. qle1 = new QLineEdit(this);
    10. qle1->setGeometry(120,20,200,40);
    11. }
    12. Form::~Form()
    13. {
    14. //delete ui;
    15. }
    16. void Form::getData(QString str)
    17. {
    18. qle1->setText(str);
    19. }

    main.cpp

    1. #include "form.h"
    2. #include"widget.h"
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9. return a.exec();
    10. }

    结果展示:

     运行展示widget界面,点击发送就可以跳转到form界面,并且成功传递参数。

     

     总结一下:

    在widget.h声明一个发送信号,

    signals:   
     void sendData(QString);//发送数据的信号,不需要实体函数,在头文件定义就可以    

    然后定义槽函数

    private slots:    
    void clicked_button();//定义的槽函数

    在widget.cpp中,实现槽函数

    void clicked_button()

    {

            //函数实现...

        emit sendData(strss);//发送数据,使用emit
    }

    在widget.cpp合适位置设置connect函数实现,绑定,测试是在构造函数中使用

        //连接信号和槽函数的重点,f是指定页面的一个指针           connect(this,SIGNAL(sendData(QString)),f,SLOT(getData(QString)));
    

    在form.h中定义,接收槽函数

    private slots:    
     void getData(QString);//得到数据的槽函数  

    在form.cpp中实现

    void Form::getData(QString str)
    { 
       //实现代码
    }
    
  • 相关阅读:
    【物联网】MATLAB通过MQTT与阿里云和本地服务器建立连接
    新手入门:Web安全测试大盘点
    SpringBoot 入门
    LeetCode 3. 无重复字符的最长子串
    python多条曲线拟合成一条
    PCA(Principal Component Analysis,主成分分析)与矩阵X的协方差矩阵之间的联系
    java计算机毕业设计汇美食电子商城源码+mysql数据库+系统+LW文档+部署
    微信小程序和H5之间互相跳转、互相传值
    【Java基础】数组
    Visual Studio 2019下使用C++与Python进行混合编程——环境配置与C++调用Python API接口
  • 原文地址:https://blog.csdn.net/m0_61973596/article/details/126767740