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

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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on 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
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on 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.

        JoeCFDJ JonBJ 2 Replies Last reply
        0
        • mzimmersM mzimmers

          This doesn't work either:

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

          I think I'm overlooking something fairly basic here.

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on 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
          • mzimmersM mzimmers

            This doesn't work either:

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

            I think I'm overlooking something fairly basic here.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #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 :)

            mzimmersM 1 Reply Last reply
            1
            • JonBJ JonB

              @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 :)

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by mzimmers
              #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());
              
              JoeCFDJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @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());
                
                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

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

                mzimmersM 1 Reply Last reply
                1
                • JoeCFDJ JoeCFD

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

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by mzimmers
                  #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.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • mzimmersM mzimmers

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

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #9

                    @mzimmers

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

                    mzimmersM JonBJ 2 Replies Last reply
                    1
                    • JoeCFDJ JoeCFD

                      @mzimmers

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

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on 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
                      • JoeCFDJ JoeCFD

                        @mzimmers

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @JoeCFD said in QVariant to QList:

                        got this from ChatGPT

                        Depressing! :(

                        1 Reply Last reply
                        0
                        • JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by JoeCFD
                          #12

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

                          mzimmersM 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

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

                            mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on 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

                            • Login

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