【问题标题】:QObject connect error with std::vector<int>QObject 与 std::vector<int> 连接错误
【发布时间】:2021-12-27 09:23:57
【问题描述】:

我有一个包含 int 向量的结构,该结构稍后在类中使用,这会生成带有错误消息的运行时错误,

QObject::connect: 无法对“QVector”类型的参数进行排队(确保使用 qRegisterMetaType() 注册了“QVector”)

结构:

struct Grid {

  std::vector<int> indices;
  int pt_count{};

  Grid()=default;
  ~Grid()=default;

  void add_indice(int _indice) {
    indices.push_back(_indice);
    pt_count++;
  }
};

main.cpp

#include <vector>
#include <struct.h>

int main() {
  std::vector<Grid> grids;
  grids.resize(5);

  for (int i{0}; i < 5; i++){
    for (int j {0}; j < 10; j++){
      grids.at(i).add_indice(j);
    }
  }

  return 0;    
}

为什么会出现上述错误?有人可以帮忙吗?

谢谢!

【问题讨论】:

标签: c++ vector runtime-error qvector


【解决方案1】:

该消息是关于 QVector 的,与 std::vector 无关(至少在撰写本文时没有)。

在某处添加(在该错误发生之前,可能在main),例如:

#include <QVector>
#include <QMetaType>

// ...

void myFunc() {
    int metatype_id = qRegisterMetaType< QVector >("QVector");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2020-01-26
    • 2012-04-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多