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. How to change QVector to QString
Qt 6.11 is out! See what's new in the release blog

How to change QVector to QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.4k Views 2 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    My paint project need save and read DashPattern.

    Basic use:
    QVector<qreal> pattern { 5,5 };
    pen.setDashPattern(pattern);

    QVector to QString needed:
    settings.setValue("DashPattern", pattern);

    QString to QVector needed:
    QVector<qreal> pattern = settings.value(("DashPattern", "{ 5,5 }";

    https://github.com/sonichy

    sierdzioS 1 Reply Last reply
    0
    • sonichyS sonichy

      My paint project need save and read DashPattern.

      Basic use:
      QVector<qreal> pattern { 5,5 };
      pen.setDashPattern(pattern);

      QVector to QString needed:
      settings.setValue("DashPattern", pattern);

      QString to QVector needed:
      QVector<qreal> pattern = settings.value(("DashPattern", "{ 5,5 }";

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @sonichy "settings" are QSettings? You don't need to change this to QString, you need a QVariant.

      constexpr auto PatternKey = QLatin1String("DashPattern");
      settings.setValue(PatternKey, QVariant::fromValue(pattern));
      // ...
      auto pattern = settings.value(PatternKey).value<QVector<qreal>>();
      

      (Z(:^

      sonichyS 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @sonichy "settings" are QSettings? You don't need to change this to QString, you need a QVariant.

        constexpr auto PatternKey = QLatin1String("DashPattern");
        settings.setValue(PatternKey, QVariant::fromValue(pattern));
        // ...
        auto pattern = settings.value(PatternKey).value<QVector<qreal>>();
        
        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @sierdzio I need set the value by QLineEdit:
        settings.setValue("DashPattern", lineEdit->text());

        https://github.com/sonichy

        sierdzioS C 2 Replies Last reply
        0
        • sonichyS sonichy

          @sierdzio I need set the value by QLineEdit:
          settings.setValue("DashPattern", lineEdit->text());

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Code you wrote now is correct but seems unrelated to the original question, I am confused.

          (Z(:^

          1 Reply Last reply
          1
          • sonichyS sonichy

            @sierdzio I need set the value by QLineEdit:
            settings.setValue("DashPattern", lineEdit->text());

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @sonichy said in How to change QVector to QString:

            I need set the value by QLineEdit:

            Why?

            If you are using Qt6 then:

            QVector<qreal> pattern { 5,5 };
            constexpr auto PatternKey = QLatin1String("DashPattern");
            settings.setValue(PatternKey, QVariant::fromValue(pattern));
            

            will save something to QSettings that can be restored using:

            auto pattern = settings.value(PatternKey).value<QVector<qreal>>();
            

            If the QSetting backing store is an INI file then it might look like this:

            $ cat test.ini 
            [General]
            DashPattern=@Variant(\0\0\0\x7f\0\0\0\xeQList<double>\0\0\0\0\x2@\x14\0\0\0\0\0\0@\x14\0\0\0\0\0\0)
            

            but you really do not need to worry about that.

            sonichyS 1 Reply Last reply
            1
            • C ChrisW67

              @sonichy said in How to change QVector to QString:

              I need set the value by QLineEdit:

              Why?

              If you are using Qt6 then:

              QVector<qreal> pattern { 5,5 };
              constexpr auto PatternKey = QLatin1String("DashPattern");
              settings.setValue(PatternKey, QVariant::fromValue(pattern));
              

              will save something to QSettings that can be restored using:

              auto pattern = settings.value(PatternKey).value<QVector<qreal>>();
              

              If the QSetting backing store is an INI file then it might look like this:

              $ cat test.ini 
              [General]
              DashPattern=@Variant(\0\0\0\x7f\0\0\0\xeQList<double>\0\0\0\0\x2@\x14\0\0\0\0\0\0@\x14\0\0\0\0\0\0)
              

              but you really do not need to worry about that.

              sonichyS Offline
              sonichyS Offline
              sonichy
              wrote on last edited by
              #6

              @ChrisW67 How to change the dash value ? So I need QLineEdit and QVector QString exchange.

              https://github.com/sonichy

              M 1 Reply Last reply
              0
              • sonichyS sonichy

                @ChrisW67 How to change the dash value ? So I need QLineEdit and QVector QString exchange.

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @sonichy said in How to change QVector to QString:

                how to change the dash value ? So I need QLineEdit and QVector QString exchange.

                You can create a float vector like this:

                QString text="1.2, 2.4, 5.75, 10.25";
                QStringList list=text.split(',');
                QVector<float> fVec;
                
                for(QString& s : list) fVec<<s.toFloat();
                	
                qDebug()<<fVec;  // --> QVector(1.2, 2.4, 5.75, 10.25)
                
                sonichyS 1 Reply Last reply
                2
                • M mpergand

                  @sonichy said in How to change QVector to QString:

                  how to change the dash value ? So I need QLineEdit and QVector QString exchange.

                  You can create a float vector like this:

                  QString text="1.2, 2.4, 5.75, 10.25";
                  QStringList list=text.split(',');
                  QVector<float> fVec;
                  
                  for(QString& s : list) fVec<<s.toFloat();
                  	
                  qDebug()<<fVec;  // --> QVector(1.2, 2.4, 5.75, 10.25)
                  
                  sonichyS Offline
                  sonichyS Offline
                  sonichy
                  wrote on last edited by sonichy
                  #8

                  @mpergand I accept your code
                  https://gitlink.org.cn/sonichy/HTYPaint/commits/a3275f0062

                  https://github.com/sonichy

                  JonBJ 1 Reply Last reply
                  0
                  • sonichyS sonichy

                    @mpergand I accept your code
                    https://gitlink.org.cn/sonichy/HTYPaint/commits/a3275f0062

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

                    @sonichy
                    What is your actual question/problem? If you have a list of numbers (integers or floats) and you want to save it to settings as a string and restore it as a list of numbers, and for some reason you don't want to do it via a QVariant like @ChrisW67 suggested, (is that what you want?) you need to:

                    • To save, you need to iterate the list calling QString::number() on each one to build up a comma (or other character)-separated string, then pass that to settings.
                    • To restore, you need to call QString.split() to split the string into items and call QString::toInt/Float() on each one to convert back to numbers.

                    Of course, if you have the numbers stored as a separated string in your text edit or elsewhere and you only want to deal with that then you don't need to do joining/splitting/converting from/to numbers.

                    UPDATE

                    @sonichy said in How to change QVector to QString:

                    @mpergand I accept your code

                    You have gone back and changed your post to say "I accept your code" now, maybe you mean you have no problem, I don't know.

                    1 Reply Last reply
                    2

                    • Login

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