Important: Please read the Qt Code of Conduct -

QList with multiple types

  • Hi!

    I'd like to know whether it's possible to have a QList with multiple types, like
    QList <QString, QString, bool>

    When I try to compile it, I get this message:
    error: wrong number of template arguments (3, should be 1)
    error: provided for 'template<class T> class QList'

    I understand why it says so, but I don't know what to do to get what I want. Can someone give me some help?

    Thank you very much!

  • you should use QList "<QVariant>":
    with QVariant you can register own data-types...
    if you need QString, QString, bool in the row(one QList element), this would look like that:

    struct MyStruct
    QString str1;
    QString str2;
    bool state;


    QList<QVariant> list;
    MyStruct tmp;
    tmp.str1 = "test1";
    tmp.str2 = "test2";
    tmp.state = true;



    MyStruct tmp2 =<MyStruct>();

  • Hello,

    you can create a struct

    struct yourNewType {
    QString ...;
    QString ...;
    bool ...;

    and than use this type in QList


  • Hi,
    You can have a look on "QPair": and use
    QList< QPair<QString,QString,bool> >.

    Check this if it fulfills your requirement.

  • [quote author="Soumitra" date="1336656040"]Hi,
    You can have a look on "QPair": and use
    QList< QPair<QString,QString,bool> >[/quote] As the name suggests, a QPair can't contain a triplet. You'd have to use absurd constructs like QList<QPair<QString,QPair<QString,bool> > > to make that work, but I won't encourage that. I'd go for a struct or class.

  • @DerManu,
    Thanks for the clarification, I didn't tried with QPair yet. Your answer improved my knowledge base as well.

    Thanks a lot :)

Log in to reply