Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:24:41+00:00 2026-06-13T10:24:41+00:00

A QVariant is holding a QMap object that is to be converted into a

  • 0

A QVariant is holding a QMap object that is to be converted into a custom type, MyClass or MyClass2.

Example:

class MyClass{
   int item1;
   int item2;
   QString string1;
   AnotherClass subclass;
};

class MyClass2{
   int item1;
   QString string1;
   AnotherClass subclass;
};

functions have been written to convert the QVariant to the associated classes

MyClass QVariantToMyClass1(QVariant);
MyClass2 QVariantToMyClass1(QVariant);

My question is, in a template function what is the proper way to pass in the function pointer? The code shown below returns an error ‘const class QVariant has no member named convFunct’

template<class T>
QList<T> QVariantToQList(QVariant & qv,T (* convFunct)() )
{
    // Create the list that will hold the return values
    QList<T> qListOfMembers;
    if(qv.typeName() == "QVariantMap"){
        foreach(QVariant const& mapMember,qv.toMap())
        {
            qListOfMembers.append(mapMember.convFunct());
        }
    }
    else if (qv.typeName() == "QVariantList"){
        foreach(QVariant const& listMember,qv.toList())
        {
            qListOfMembers.append(listMember.convFunct());
        }
    }
    else
    {
        qDebug()<< "QVariantToQList currently is implemented only for QMap and QList types";
        throw ;
    }
    return qListOfMembers;
}

This is a follow-up question to a previous question The difference between that question and this one is the T is ‘MyClass’ or ‘MyClass2’ instead of a type that is normally held by a QVariant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:24:42+00:00Added an answer on June 13, 2026 at 10:24 am

    If I understand your question correctly, convFunct supposed to be a function that get a QVariant and return an instance of MyClass or MyClass2, is it correct? if your answer is yes, then this function should get a parameter of type QVariant and your function get no parameter, so the result is:

    template<class T>
    QList<T> QVariantToQList(QVariant & qv,T (*convFunct)(QVariant const&) )
    {
        // Create the list that will hold the return values
        QList<T> qListOfMembers;
        if(qv.typeName() == "QVariantMap"){
            foreach(QVariant const& mapMember,qv.toMap())
            {
                qListOfMembers.append(convFunct(mapMember));
            }
        }
        else if (qv.typeName() == "QVariantList"){
            foreach(QVariant const& listMember,qv.toList())
            {
                qListOfMembers.append(convFunct(listMember));
            }
        }
        else
        {
            qDebug()<< "QVariantToQList currently is implemented only for QMap and QList types";
            throw ;
        }
        return qListOfMembers;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

QVariant (needed for QSettings class) supports creation from QMap<QString, QVariant> But trying to initialise
I have a custom type I'd like to use with QVariant but I don't
I want to know, How QVariant can internally stores, int, QMap, QList,... I mean
I've got a property of a custom type. class foo : public QObject {
I have a levelObjects object that's a QList of QVariants of QMaps: QList< QVariant
#include <QtCore/QCoreApplication> #include <QVariant> #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc,
Here's the demo code: QList<Custom> L; QVariant v(QVariant::fromValue(l)); QDataStream d; d << v; The
My question refers to: How to verify QVariant of type QVariant::UserType is expected type?
is there a quick way to read record from query as a QMap<QString,QVariant> or
I have a QVariant object within a QTreeWidgetItem, how can I cast it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.