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. QVariant to QList
Forum Update on Monday, May 27th 2025

QVariant to QList

Scheduled Pinned Locked Moved Solved General and Desktop
c++model
13 Posts 3 Posters 2.1k Views
  • 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 9 Jan 2023, 23:19 last edited by
    #1

    Hi all -

    I'm fooling around with a Model, and am trying to figure out how to correctly code the setter. Here's the relevant code:

    struct ActivityItem {
        QString name;
        QList<QString> equipment;
        bool recommended;
    };
    
    bool ActivityModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        switch (role) {
            item.equipment.clear();
            item.equipment.append(value.toList()); // error: No matching member function for call to 'append'
    

    The last line isn't right, but the error message isn't very helpful, and the code seems to match the doc (as far as I can tell). Can someone give me an idea of what I'm doing wrong here?

    Thanks...

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 9 Jan 2023, 23:26 last edited by
      #2

      It seems that value.toList() returns QList<QVariant>, not QList<QString>. You may need explicit conversion.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mzimmers
        wrote on 9 Jan 2023, 23:32 last edited by
        #3

        This doesn't work either:

        QList<QString> qls = static_cast<QList<QString>>(value.toList());
        

        I think I'm overlooking something fairly basic here.

        J J 2 Replies Last reply 9 Jan 2023, 23:40
        0
        • M mzimmers
          9 Jan 2023, 23:32

          This doesn't work either:

          QList<QString> qls = static_cast<QList<QString>>(value.toList());
          

          I think I'm overlooking something fairly basic here.

          J Offline
          J Offline
          JoeCFD
          wrote on 9 Jan 2023, 23:40 last edited by
          #4

          @mzimmers said in QVariant to QList:

          QList<QString> qls = static_cast<QList<QString>>(value.toList());

          I am not sure static_cast works. Can you try to convert one after another with toString() in the list?

          1 Reply Last reply
          1
          • M mzimmers
            9 Jan 2023, 23:32

            This doesn't work either:

            QList<QString> qls = static_cast<QList<QString>>(value.toList());
            

            I think I'm overlooking something fairly basic here.

            J Offline
            J Offline
            JonB
            wrote on 9 Jan 2023, 23:46 last edited by JonB 1 Sept 2023, 23:48
            #5

            @mzimmers
            I don't think a list of variants is/can be the same thing as a list of strings, or converted between the two/treated as such? (A QVariant cannot be a QString, it can only contain one.) My thought is you would want to iterate, appending each element?
            Note that this is purely a guess as I read this :)

            M 1 Reply Last reply 9 Jan 2023, 23:49
            1
            • J JonB
              9 Jan 2023, 23:46

              @mzimmers
              I don't think a list of variants is/can be the same thing as a list of strings, or converted between the two/treated as such? (A QVariant cannot be a QString, it can only contain one.) My thought is you would want to iterate, appending each element?
              Note that this is purely a guess as I read this :)

              M Offline
              M Offline
              mzimmers
              wrote on 9 Jan 2023, 23:49 last edited by mzimmers 1 Oct 2023, 00:54
              #6

              @JonB I think you're saying basically the same thing that @JoeCFD is, and I think you're both right.

              But I still don't know how to cast/convert my QVariant value into a QString (or a QList<QString>).

              EDIT: though this comes close:

                          for (QString qs : value.toStringList()) {
                              item.equipment.append(qs);
                          }
              

              EDIT 2: which means this comes even closer:

                      case EquipmentRole:
                          item.equipment.clear();
                          item.equipment.append(value.toStringList());
              
              J 1 Reply Last reply 9 Jan 2023, 23:56
              0
              • M mzimmers
                9 Jan 2023, 23:49

                @JonB I think you're saying basically the same thing that @JoeCFD is, and I think you're both right.

                But I still don't know how to cast/convert my QVariant value into a QString (or a QList<QString>).

                EDIT: though this comes close:

                            for (QString qs : value.toStringList()) {
                                item.equipment.append(qs);
                            }
                

                EDIT 2: which means this comes even closer:

                        case EquipmentRole:
                            item.equipment.clear();
                            item.equipment.append(value.toStringList());
                
                J Offline
                J Offline
                JoeCFD
                wrote on 9 Jan 2023, 23:56 last edited by
                #7

                @mzimmers item.equipment.append(value.toStringList()); is a good one. Does it work?

                M 1 Reply Last reply 10 Jan 2023, 00:06
                1
                • J JoeCFD
                  9 Jan 2023, 23:56

                  @mzimmers item.equipment.append(value.toStringList()); is a good one. Does it work?

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 10 Jan 2023, 00:06 last edited by mzimmers 1 Oct 2023, 00:14
                  #8

                  @JoeCFD I can't tell for sure until I can expose this in QML, but I'm fairly sure it's working. I'll ask my other question in the QML forum.

                  Thanks for the help, guys.

                  EDIT: yeah, it seems to have worked.

                  J 1 Reply Last reply 10 Jan 2023, 00:20
                  0
                  • M mzimmers
                    10 Jan 2023, 00:06

                    @JoeCFD I can't tell for sure until I can expose this in QML, but I'm fairly sure it's working. I'll ask my other question in the QML forum.

                    Thanks for the help, guys.

                    EDIT: yeah, it seems to have worked.

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 10 Jan 2023, 00:20 last edited by JoeCFD 1 Oct 2023, 00:20
                    #9

                    @mzimmers

                    got this from ChatGPT and you are good.
                    if (variant.canConvert<QStringList>()) {
                    QList<QString> stringList = variant.toStringList();
                    }

                    M J 2 Replies Last reply 10 Jan 2023, 00:22
                    1
                    • J JoeCFD
                      10 Jan 2023, 00:20

                      @mzimmers

                      got this from ChatGPT and you are good.
                      if (variant.canConvert<QStringList>()) {
                      QList<QString> stringList = variant.toStringList();
                      }

                      M Offline
                      M Offline
                      mzimmers
                      wrote on 10 Jan 2023, 00:22 last edited by
                      #10

                      @JoeCFD yeah, I saw the canConvert() method in the docs, but forgot to add it to my code. Thanks for the reminder.

                      1 Reply Last reply
                      0
                      • J JoeCFD
                        10 Jan 2023, 00:20

                        @mzimmers

                        got this from ChatGPT and you are good.
                        if (variant.canConvert<QStringList>()) {
                        QList<QString> stringList = variant.toStringList();
                        }

                        J Offline
                        J Offline
                        JonB
                        wrote on 10 Jan 2023, 12:40 last edited by
                        #11

                        @JoeCFD said in QVariant to QList:

                        got this from ChatGPT

                        Depressing! :(

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JoeCFD
                          wrote on 10 Jan 2023, 14:57 last edited by JoeCFD 1 Oct 2023, 15:31
                          #12

                          Understand. Think about you can never be the number 1 or even close anymore if you play chess.

                          M 1 Reply Last reply 10 Jan 2023, 14:59
                          0
                          • J JoeCFD
                            10 Jan 2023, 14:57

                            Understand. Think about you can never be the number 1 or even close anymore if you play chess.

                            M Offline
                            M Offline
                            mzimmers
                            wrote on 10 Jan 2023, 14:59 last edited by
                            #13

                            @JoeCFD said in QVariant to QList:

                            Understand. Thiink about you can never be number 1 anymore if you play chess.

                            Oddly enough, that was never a concern of mine.

                            1 Reply Last reply
                            1

                            5/13

                            9 Jan 2023, 23:46

                            topic:navigator.unread, 8
                            • Login

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