Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

changing a value in a const funktion



  • Hi,

    I have the following problem.
    I want to change a value of an variable in a const funktion.
    Therefore I made the variable mutalbe

    .h

    ...
    private:
        mutable qint32 limit;
    ...
    

    The new value I get via signals and slots, in the Slot it works, but to change the value in the funktion does not work.
    I know that the funktion is const but therefore the variabel is mutable.
    I'm now expert in programming C++ and hope that somewone can help me, what I'm doing wrong.

    .cpp

    QVariant TableCustomise::data(const QModelIndex &index, int role) const
    {
    ...
            if (index.column() == 0)
            {
                return colorData(index);
            }
     ...
    
    }
    
    
    QString TableCustomise::colorData(const QModelIndex &index)const
    {
    
        qint32 contentForColor = index.sibling(index.row(), index.column() + 2).data(Qt::EditRole).toInt();
    
        QString color = "";
        if (index.column() == 0)
        {
            if (limit <= contentForColor  )
            {
                 qDebug()<<"limit in Fkt" << limit;
                 color = "green";
            }
    
        }
            else if (contentControlToolNumber == 0)
            {
                color = "gray";
            }
        }
        return color;
    }
    
    void TableCustomise::receiveLimit(qint32 receiveLimit)
    {
        limit = receiveLimit;
        qDebug()<<"limit Customise bei Übergabe :"<< limit;
    }
    
    

    Thank you in advance.

    Regards



  • @Mogli123
    Don't understand what exactly you claim is not working/happening? If your variable in mutable --- regardless of whether that's a good idea --- then it can be altered, it does not matter where there happens from. So what are you saying: are you getting a compile-time error or not seeing a value you expect at runtime?

    BTW, if the only variable receiveLimit() wants to alter in this is mutable, then receiveLimit() can be marked const too.



  • Tou are not changing limit inside any const method you posted



  • Thank you for your help.

    compiling works without no errors
    to change the value limit in the "colorData " funktion works too

    In the funktion "contentForColor" the limit value does not change



  • @Mogli123 said in changing a value in a const funktion:

    In the funktion "contentForColor"

    • contentForColor is a variable, not a function.
    • Inside TableCustomise::colorData you never assign anything to limit so it can't change


  • @VRonin

    contentForColor is a variable, not a function.

    oh sorry you i right

    OK, I understand, but I do not know the value. How can I implement the transfer of the values?
    Do you have any idea?



  • @Mogli123 said in changing a value in a const funktion:

    . How can I implement the transfer of the values?
    Do you have any idea?

    Just put limit on the left of an =. For example: limit = 5;



  • @VRonin
    But I do not know the value, as shown in your example, for the variable limit therefore I have to change the value with a variable.
    And I do not know how to get the new value in the funktion.

    I hope you understand what I mean.



  • @Mogli123 said in changing a value in a const funktion:

    I hope you understand what I mean.

    I'm sorry but I don't...


  • Lifetime Qt Champion

    @Mogli123 said in changing a value in a const funktion:

    And I do not know how to get the new value in the funktion

    which value in which function?
    Your description is really unclear and confusing.
    Can you explain better and show in code what you want to do and where?



  • Sorry for my bad explanation
    @VRonin
    @jsulm
    @JonB

    I'll try again:

    I create the following variable

    tableCustomise.h

    ...
    private:
        mutable qint32 limit;
    ...
    

    I get the value for this variable via signals and slots from another .cpp file, as the value can change

    this is the public slot where I get the value
    tableCustomise.cpp

    void TableCustomise :: receiveLimit (qint32 receiveLimit)
    {
    limit = receiveLimit;
    qDebug () << "limit Adjust on transfer:" << limit;
    }
    

    The following function is the function where I need to know the current value from limit
    tableCustomise.cpp

    QString TableCustomise :: colorData (const QModelIndex & index) const
    {
    
    qint32 contentForColor = index.sibling (index.row (), index.column () + 2) .data (Qt :: EditRole) .toInt ();
    
    QString color = "";
    if (index.column () == 0)
    {
    if (limit <= contentForColor)
    {
    qDebug () << "limit in Fkt" << limit;
    color = "green";
    }
    
    }
    else if (contentControlToolNumber == 0)
    {
    color = "gray";
    }
    }
    Return color;
    }
    

    and the last function calls the colorData function

    QVariant TableCustomise :: data (const QModelIndex & Index, int role) const
    {
    ...
    if (index.column () == 0)
    {
    return colorData (index);
    }
    ...
    
    }
    

    My problem is that the function is constant, so the value of the variable does not change in the colorData function.
    I tried it with the keyword mutable as in the example above, but that does not work (you already explained the reason)

    Now I have no idea how to get the required value of limit to the function color Data

    I hope that my problem is clear now.

    Many thanks for your help


  • Lifetime Qt Champion

    @Mogli123 If colorData changes the objects state (it changes a member variable) then it can't be const, just make it normal non const method.
    "The following function is the function where I need to know the current value from limit" - this function is member of the same class, so it has direct access to "limit".
    Also, I can't see where in colorData() you're trying to change "limit".



  • My problem is that the function data is a virtual function and it have to be constant. Therefore I have no choice, or no other reason which I know

    the method data is from the QSqlQueryModel Class

    I do not change the variable limit in the method colorData
    I only changed it in the public slot because I thought that the value limit because of that changes in the whole .cpp file but that seems that it is not the right way


  • Lifetime Qt Champion

    I don't see where you change 'limit' member var inside TableCustomise::colorData() so what's the problem?


  • Lifetime Qt Champion

    @Mogli123 said in changing a value in a const funktion:

    I only changed it in the public slot because I thought that the value limit because of that changes in the whole .cpp file but that seems that it is not the right way

    Why is it not the right way?
    Do you have to change limit inside colorData()?



  • @jsulm said in changing a value in a const funktion:

    Why is it not the right way?

    Because it does not work...^^

    Do you have to change limit inside colorData()?

    No, I have only to know the current value from limit for the if condition in colorData()


  • Lifetime Qt Champion

    @Mogli123 said in changing a value in a const funktion:

    Because it does not work

    Then why don't you try to find out why it doesn't work instead of trying to change the value in a const method where you don't even have access to the new value?

    To find out why it doesn't work (you did not explain in what way it does not work):

    • Did you make sure the slot receiveLimit() was called?

    • If it was called did you check the values of receiveLimit and limit (before assigning new value to limit)?



  • @jsulm
    with qDebug() I checked if the new limit is received and that works.
    Only in the constant method colorData the value limit is unchanged
    I hoped that the value limit changes in the whole tableCustomise.cpp file, but that does not work because the method colorData is const, but it have it to made the method const, how I mentioned above. (I'm right?)
    I have no idea how I can get the current value from limit to the const method for checking if the if condition is right or not

    I have no many experience in C++ programming and I hoped that someone can show me a way to solve my problem.


  • Moderators

    @Mogli123
    I still don't see what is causing the problem

    QString TableCustomise :: colorData (const QModelIndex & index) const

    A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variable is okay.

    You only read limit, -> make limit a normal member



  • @J-Hilk

    however, reading of a class variable is okay.

    but the value in the const function colorData from limit is not the right value.
    Only in the public slot receiveLimit the value from limit is right.

    You only read limit, -> make limit a normal member

    Do you mean to remove mutable?
    If yes, at the beginning I tryed it without the key word


  • Lifetime Qt Champion

    @Mogli123 There must be something else in your code. If the slot is called and you assign new value to limit then it should have the new value.
    Do you have more than one instance of TableCustomise?
    Can you show where you create the instance?
    Can you show where you connect the signal to receiveLimit() slot?
    Is it possible that you have more than one instance of TableCustomise and connect the slot receiveLimit() from one but use limit in another one?
    Do you have any other definitions of limit?
    Is this really the code you have:

    void TableCustomise :: receiveLimit (qint32 receiveLimit)
    {
        limit = receiveLimit;
        qDebug () << "limit Adjust on transfer:" << limit;
    }
    

    ?



  • @jsulm

    Thank you very much, that was it
    I had a second instance of tableCustomise...

    @J-Hilk
    @Christian-Ehrlicher
    @VRonin
    @JonB
    Also thank you, too

    sorry for my bad explenation
    have a nice day


  • Lifetime Qt Champion

    @Mogli123 said in changing a value in a const funktion:

    I had a second instance of tableCustomise

    This is an issue I often see here, so you're not the first one who oversees it and for sure not the last one :-)
    Can happen :-)


Log in to reply