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>":http://doc-snapshot.qt-project.org/4.8/qvariant.html
    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;
    };

    Q_DECLARE_METATYPE(MyStruct)

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

    list.append(QVariant(tmp));

    ...

    MyStruct tmp2 = list.at(0).value<MyStruct>();
    @



  • Hello,

    you can create a struct

    @
    struct yourNewType {
    QString ...;
    QString ...;
    bool ...;
    };
    @

    and than use this type in QList

    @
    QList<yourNewType>
    @



  • Hi,
    You can have a look on "QPair":http://qt-project.org/doc/qt-4.8/QPair.html 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":http://qt-project.org/doc/qt-4.8/QPair.html 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.