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 4242792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:29:25+00:00 2026-05-21T03:29:25+00:00

Can I define a QList such that it holds a collection of templated structs,

  • 0

Can I define a QList such that it holds a collection of templated structs, with each struct defined on different type ??

Consider:

template<typename T>struct AA
{
  T value;
}

Can I declare a QList such that it holds different instances of AA ??
something like:

struct<int> myIntStruct;
myIntStruct.value = 10;
struct<double> myDobleStruct;
myDoubleStruct = 12.2;

template<typename T>
QList<struct AA<T>> myList;
myList.push_back(myIntStruct);
myList.push_back(myDoubleStruct);

My Opinion is that the QList should contain entities of same datatype (even for a templated objects) and that’s why the above operation is illegal. In that case what structure can I use in Qt to do an operation like that ??

Thanks,

Vishnu.

  • 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-05-21T03:29:26+00:00Added an answer on May 21, 2026 at 3:29 am

    Why you would want a container to have dissimilar objects is probably a design flaw. However some languages support this such as Smalltalk which has the concept of a Bag Collection that can contain a mix of different object types. Qt may have some sort of bag container class already but I am unaware of it’s existence.

    This concept of mixed types in a single container does not work so well in the strongly typed world of C++. Variant objects are the closest thing and even then not for non integral types without making your own “super variant” class which you could do by subclassing QVariant then adding some bits to handle your structs.

    Again ask yourself the question why do you need this functionality? A simpler solution implemented elsewhere will probably alleviate the need for such a container.

    Edit: Quickie example of QMap<QString, QVariant> to help determine if that would work.

    class SomeObject
    {
    public:
        SomeObject() // default
            : someInt(0), someDouble(0) {}
        SomeObject(int i, double d) // explicit
            : someInt(i), someDouble(d) {}
    
        int GetSomeInt() const { return someInt; }
        double GetSomeDouble() const { return someDouble; }
    
    private:
        int someInt;
        double someDouble;
    };
    
    // must be outside of namespace
    Q_DECLARE_METATYPE(SomeObject)
    
    // then you can do stuff like this:
        QMap<QString, QVariant> mapNameToValue;
    
        // populate map
        mapNameToValue["UserName"] = "User";
        mapNameToValue["Port"] = 10101;
        mapNameToValue["PI"] = 3.14159265;
        mapNameToValue["DateTime"] = QDateTime::currentDateTime();
        QVariant userValue;
        userValue.setValue(SomeObject(5, 34.7));
        mapNameToValue["SomeObject"] = userValue;
    
        // read from map
        QString userName = mapNameToValue["UserName"].toString();
        unsigned int port = mapNameToValue["Port"].toUInt();
        double PI = mapNameToValue["PI"].toDouble();
        QDateTime date = mapNameToValue["DateTime"].toDateTime();
        SomeObject myObj = mapNameToValue["SomeObject"].value<SomeObject>();
    
        int someInt = myObj.GetSomeInt();
        double someDouble = myObj.GetSomeDouble();
    

    QVariant handles a lot of types it also has template methods setValue and value<> for user defined types. As shown briefly above you have to use Q_DECLARE_METATYPE macro to register it with QMetaType and provide a default constructor in case value<> conversion fails. It won’t support certain comparison operators so I would make my own version of QVariant and QMetaType to play nice with any extra types needed so it feels more consistent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that we can define two types linking each other, for instance: type
I know I can define new structs in ruby by doing Person = Struct.new(:first_name,
I know I can define file type using in appinfo.plist such as zip, pdf
In C# we can define a generic type that imposes constraints on the types
How do i can define a type List so that i could use it
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
In Makefile 's I can define tasks so that a user can specify what
I saw that in Eclipse I can define User Libraries - to make setting
In java we can define max memory that process can take using xmx parameter.
Given that one can define a class in VB Script, is there any way

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.