• 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)
    { 
       //实现代码
    }
    
  • 相关阅读:
    通过Nacos配置刷新进行RabbitMQ消费者在线启停
    Vue源码学习(十二):列队处理(防抖优化,多次调用,只处理一次)
    基于ASP.NET+MYSQL的医院信息管理系统
    外包干了3个多月,技术退步明显。。。。。
    ajax发送json格式与文件数据、django自带的序列化器(了解)
    使用弹簧启动和 JPA 测试乐观锁定处理
    驱动保护进程 句柄降权 杀软自保 游戏破图标技术原理和实现
    Windows环境下的ELK——filebeat+logstash+elasticsearch(4)
    《昇思25天学习打卡营第17天|K近邻算法实现红酒聚类》
    How to clean up Graylog Default index set log
  • 原文地址:https://blog.csdn.net/m0_61973596/article/details/126767740