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 Updated to NodeBB v4.3 + New Features

QVariant to QList

Scheduled Pinned Locked Moved Solved General and Desktop
c++model
13 Posts 3 Posters 2.3k 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.
  • 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

                        12/13

                        10 Jan 2023, 14:57

                        • Login

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