Demaunt
Demaunt

Reputation: 1243

qvariant_cast<std::string> returns empty string

I am using QT 5.7 and MVSC - 2010 (yes, it is old, but I have to due to project)

I have a template function

template<T>
T foo (QString qs)
{
    return qvariant_cast<T>(qs);
}

If I make

void main()
{
    std::string str = "42"
    QString qss = QString::fromStdString(str);
    std::string another_str = foo<std::string>(qss );
}

Then another_str will be ""; While toStdString method works perfectly. What is the problem of qvariant_cast?

P.S. I have declared qt meta type, so qvariant_cast is compling, but returning empty string.

Upvotes: 0

Views: 187

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38538

According to the manual call qss.canConvert<std::string>() to find out whether a type can be converted. If the value cannot be converted, a default-constructed value will be returned. It is your case, the empty std::string is returned.

Upvotes: 1

Related Questions