Question

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>

Came to know about Q_DECLARE_METATYPE(); It makes new types available to QVariant.

-Test Case Code-

#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 getting this error

error: could not convert ‘test1’ from ‘std::vector< QString >’ to ‘QVariant’

I am missing out something.Please Help

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top