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. [Solved]QString changing value
Forum Updated to NodeBB v4.3 + New Features

[Solved]QString changing value

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 8.2k 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.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #1

    I hope to find out how to change a value in an existing QString.

    I Try this but it will not work.
    @void MagicSettings::setValue(QString str)
    {
    value = str;
    }

    @
    And I tried to create a new object to and put in the Qstring. but nothing:
    I wonder If I have to create a pointer to a Qstring

    Maybe I have to create a pointer and point out a new QString:
    My c++ is not top of the line.

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

      [quote author="toho71" date="1328535545"]
      My c++ is not top of the line.
      [/quote]
      So, you know where to start: get a good C++ book and start with learning the basics.

      Meantime, you need to make your method take a reference or a pointer to a string instead of a simple value. In your current code, you get a copy of the string, modify it, and then the copy goes of of scope. Obvously, that does not have any effect.

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

        I tried what I think was your intention and nothing changes
        The code

        @f1.setValue(QString("No"));

        void MagicSettings::setValue(QString const& str)
        {
        value = str;
        }

        @

        and the value is declared
        @QString value
        @

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          Some complete, minimal and working example would help. It is impossible for me to tell what you are trying based on those few lines.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            I am sorry, I did not read your earlier posting well enough. I thought you wanted to change whatever you passed in the function, but it seems you want to change something else. What is value exactly? Where is is it declared?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              toho71
              wrote on last edited by
              #6

              It's declared in a class
              Here is the class

              @class MagicSettings
              {
              public:
              MagicSettings();
              MagicSettings(QString ,const QString &);
              ~MagicSettings();

              void setText(QString str);
              void setValue(const QString &str);
              void setObj(QString str);
              
              QString getText();
              const QString &getValue();
              QString getObj();
              

              private:
              QString text;
              QString value;
              QString obj;
              }; @

              Here is the
              method

              @void MagicSettings::setValue(const QString &str)
              {
              value = str;
              }
              @

              and I call for it like this

              @globalP->getSettings(vectorid).setValue(answ);
              @

              the obj is in a vector and I calls for the obj setValue and the answ is a QString;

              I have followed an exampel on the web but no.

              1 Reply Last reply
              0
              • rincewindR Offline
                rincewindR Offline
                rincewind
                wrote on last edited by
                #7

                You just say that it doesn't work, but you are not explaining what doesn't work. All I can assume is that you set the value and when you inspect the MagicSettings object the value field has not changed.

                Your problem is most likely in the getSettings method which probably returns a copy of the MagicSettings object which you change before it is destroyed.

                Make the getSettings method return a reference (&) or pointer (*) to a MagicSettings object.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  toho71
                  wrote on last edited by
                  #8

                  Thanks I'll try that and yes you assumed right about the problem.--

                  Know I changed in the cpp and .h file .
                  the getSettings method
                  @MagicSettings & GlobalParams::getSettings(int i)
                  {
                  return settingsVector.at(i);
                  }
                  @

                  I got a another error then

                  error: invalid initialization of reference of type ‘MagicSettings&’ from expression of type ‘const MagicSettings’

                  Is there something more i have to do.
                  I tried to make it like this too in the getSettingsMethod too but no good;

                  @MagicSettings &ms = settingsVector.at(i);
                  return ms;@

                  1 Reply Last reply
                  0
                  • rincewindR Offline
                    rincewindR Offline
                    rincewind
                    wrote on last edited by
                    #9

                    Where are you getting this error, on the return statement?
                    @
                    MagicSettings & GlobalParams::getSettings(int i)
                    {
                    return settingsVector.at(i);
                    }@

                    How are you storing the MagicSettings objects?
                    @std::vector<MagicSettings> settingsVector;@

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      the method "at is const for QVector":http://developer.qt.nokia.com/doc/qt-4.8/qvector.html#at
                      So you cannot return a reference to the element of that vector.
                      You can only return a const reference like

                      @const MagicSettings & GlobalParams::getSettings(int i)
                      {
                      return settingsVector.at(i);
                      }
                      @

                      However, this might have consequences on the rest of your code.

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        toho71
                        wrote on last edited by
                        #11

                        Thanks for the remainder of the [at] function:

                        used [] instead and it works.

                        Must be better to read the docs.

                        //T

                        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