Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList with multiple types
Qt 6.11 is out! See what's new in the release blog

QList with multiple types

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 10.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David_Gil
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      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>();
      @

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        task_struct
        wrote on last edited by
        #3

        Hello,

        you can create a struct

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

        and than use this type in QList

        @
        QList<yourNewType>
        @

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

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

              Thanks a lot :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved