qt send vector data to slot QVECTOR向量通过信号和槽传递数据

需求:
需要一个结构体,因为一个数据需要两个元素;
需要把该结构体装入向量,因为不知道有多少个结构体,用数组会浪费资源,扩展性不好;
需要不这个向量传递从副窗口类传递给主窗体类。

看实现
子窗口头文件:
#ifndef SPRAYER_WIDGET_H
#define SPRAYER_WIDGET_H
#include
#include
#include <fcntl.h>
#include
//需要include QmetaType

struct settings_eye_struct2
{
quint8 whichgun;
quint8 whicheye;
};
class sprayer_Widget : public QWidget
{
Q_OBJECT
signals:
void decode_eyeset_to_screen(QVector<settings_eye_struct2>);
}
子窗口CPP:
#include “sprayer_widget.h”
/struct 不能直接通过信号发送,要进行声明/
Q_DECLARE_METATYPE(settings_eye_struct2)
settings_eye_struct2 settings_eye_data2;
QVector<settings_eye_struct2>qv_eye_data2;
/其中省略很多行/
void sprayer_Widget::on_BTN_hot_gun_clicked()
{

qRegisterMetaType<QVector<settings_eye_struct2>>();
for(int i=1;i<=8;i++)
        {
            settings_eye_data2.whichgun=i;
            settings_eye_data2.whicheye=i+1;
            qv_eye_data2.push_back(settings_eye_data2);
        }
emit decode_eyeset_to_screen(qv_eye_data2);
}

主窗口头文件:
#ifndef MAIN_CONTROL_WIDGET_H
#define MAIN_CONTROL_WIDGET_H

#include “sprayer_widget.h”
class main_control_Widget : public QWidget
{
Q_OBJECT
public slots:
void test_debug(QVector<settings_eye_struct2>);
}
//这里省略一大段
};
#endif // MAIN_CONTROL_WIDGET_H

主窗口CPP:
#include “main_control_widget.h”
#include “ui_main_control_widget.h”
Q_DECLARE_METATYPE(settings_eye_struct2)
settings_eye_struct2 settings_eye_data3;
QVector<settings_eye_struct2>qv_eye_data3;

main_control_Widget::main_control_Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::main_control_Widget)
{
connect(sprayer,SIGNAL(decode_eyeset_to_screen(QVector<settings_eye_struct2>)),this,SLOT(test_debug(QVector<settings_eye_struct2>)));
}

void main_control_Widget::test_debug(QVector<settings_eye_struct2>vec_tmp)
{

for(int i = 0;i<vec_tmp.size();i++)
{
    qDebug() << "whicheye=:"<<vec_tmp.at(i).whicheye;
    qDebug() << "whichgun=:"<<vec_tmp.at(i).whichgun;
}

}
运行测试:
在这里插入图片描述
在这里插入图片描述
总结:

上面只是一个简单的实验:得到如何通过信号和槽传递向量的方法。

有些博客介绍的向量必须通过QVARIANT 容器打包传递,事实证明不需要这样做。

发布了3 篇原创文章 · 获赞 0 · 访问量 99

猜你喜欢

转载自blog.csdn.net/u013841997/article/details/104915423