(Make sure 'std::vector<std::string>' is registered using qRegisterMetaType().)
时间: 2024-04-30 16:20:43 浏览: 10
这个提示意味着您需要在使用 `std::vector<std::string>` 类型的信号与槽之前,使用 `qRegisterMetaType()` 函数注册该类型。这将使 Qt 能够正确地处理信号与槽之间的类型转换。
要注册 `std::vector<std::string>` 类型,请在您的代码中添加以下行:
```
qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
```
这将在运行时向 Qt 注册该类型。您需要在使用信号与槽之前执行此操作。
相关问题
QObject::connect: Cannot queue arguments of type 'std::vector<std::map<std::string,double> >' (Make sure 'std::vector<std::map<std::string,double> >' is registered using qRegisterMetaType().)
这个问题是因为 Qt 的信号与槽机制需要知道参数的类型信息,但是标准库中的容器类型并没有被 Qt 自动支持。因此,当你试图在信号与槽之间传递一个 std::vector<std::map<std::string,double>> 对象时,Qt 报错提示你需要使用 qRegisterMetaType() 函数将该类型注册到 Qt 的元对象系统中。在注册之后,Qt 就能够正确地识别这个类型并在信号与槽之间传递它了。
std::vector<std::map<std::string,double >>怎么给qml使用
在 QML 中使用自定义类型 `std::vector<std::map<std::string,double>>` 时,需要先将该类型注册到 Qt 的元对象系统中。以下是一个示例:
在 C++ 中,使用 `qRegisterMetaType` 函数将该类型注册到 Qt 的元对象系统中:
```c++
qRegisterMetaType<std::vector<std::map<std::string,double>>>("MyVectorMap");
```
在 QML 中,使用 `registerType` 函数将该类型注册到元对象系统中:
```qml
import QtQuick 2.0
Item {
Component.onCompleted: {
var typeId = Qt.createQmlObject('import QtQuick 2.0; QtObject {}', parent, 'TypeRegistration');
typeId.metaObject.registerType("MyVectorMap", 1, 0, "MyVectorMap");
}
}
```
现在,你就可以在 QML 中使用 `std::vector<std::map<std::string,double>>` 了:
```qml
import QtQuick 2.0
Item {
signal vectorMapSignal(variant vectorMap)
function sendVectorMap() {
var vectorMap = [
{
"key1": 1.0,
"key2": 2.0
},
{
"key3": 3.0,
"key4": 4.0
}
];
vectorMapSignal(vectorMap);
}
}
```
注意:在 QML 中使用自定义类型时,需要使用 `variant` 类型来传递该类型的对象。在上面的示例中,信号 `vectorMapSignal` 的参数类型为 `variant`,可以接收 `std::vector<std::map<std::string,double>>` 类型的对象。