问题描述
我正在研究基于QT的应用程序.我的课程之一是 QAbstractTableModel .数据函数的返回类型为 QVariant (联合).但是我要返回自定义类型std::vector<QString>
I am working on a QT based application.One of of my class is a child class of QAbstractTableModel. The data function has a return type of QVariant(Union).But i want to return a custom type std::vector<QString>
有关 Q_DECLARE_METATYPE();
的知识QVariant可用的类型.
Came to know about Q_DECLARE_METATYPE();
It makes new types available to QVariant.
-测试案例代码-
#include <QApplication>
#include <QMetaType>
#include <vector>
#include<QVariant>
Q_DECLARE_METATYPE(std::vector<QString>);
QVariant data(int role)
{
std::vector<QString> test1;
test1.push_back("Dtd");
test1.push_back("Dtd");
return test1;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
data(1);
return app.exec();
}
我收到此错误
我错过了一些东西.请帮助
I am missing out something.Please Help
推荐答案
即使您声明了新的元类型,编译器仍然会看到您正在尝试返回声明了QVariant的std::vector
.试试这个:
Even if you've declared a new metatype, the compiler still sees you're trying to return a std::vector
where you've declared returning a QVariant. Try this:
QVariant data(int role)
{
std::vector<QString> test1;
test1.push_back("Dtd");
test1.push_back("Dtd");
QVariant var;
var.setValue(test1);
return var;
}
这篇关于将函数返回的内容从"std :: vector< QString>"转换为"QVariant"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!