Important: Please read the Qt Code of Conduct -

[Solved]QString changing value

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

  • [quote author="toho71" date="1328535545"]
    My c++ is not top of the line.
    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.

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


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


    and the value is declared
    @QString value

  • Moderators

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

  • 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?

  • It's declared in a class
    Here is the class

    @class MagicSettings
    MagicSettings(QString ,const QString &);

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

    QString text;
    QString value;
    QString obj;
    }; @

    Here is the

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

    and I call for it like this


    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.

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

  • 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)

    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 =;
    return ms;@

  • Where are you getting this error, on the return statement?
    MagicSettings & GlobalParams::getSettings(int i)

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

  • the method "at is const for QVector":
    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)

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

  • Thanks for the remainder of the [at] function:

    used [] instead and it works.

    Must be better to read the docs.


Log in to reply