Solved 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 inmutable
--- 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 inthis
ismutable
, thenreceiveLimit()
can be markedconst
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 valuelimit
in the "colorData " funktion works tooIn 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 tolimit
so it can't change
-
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 variablelimit
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...
-
@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
@JonBI'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.cppvoid 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.cppQString 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
functionQVariant 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 keywordmutable
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 functioncolor Data
I hope that my problem is clear now.
Many thanks for your help
-
@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 knowthe method
data
is from theQSqlQueryModel
ClassI do not change the variable
limit
in the methodcolorData
I only changed it in the public slot because I thought that the valuelimit
because of that changes in the whole .cpp file but that seems that it is not the right way -
I don't see where you change 'limit' member var inside TableCustomise::colorData() so what's the problem?
-
@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 theif
condition incolorData()
-
@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 newlimit
is received and that works.
Only in the constant methodcolorData
the valuelimit
is unchanged
I hoped that the valuelimit
changes in the whole tableCustomise.cpp file, but that does not work because the methodcolorData
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 fromlimit
to the const method for checking if theif
condition is right or notI have no many experience in C++ programming and I hoped that someone can show me a way to solve my problem.
-
@Mogli123
I still don't see what is causing the problemQString 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
-
however, reading of a class variable is okay.
but the value in the const function
colorData
fromlimit
is not the right value.
Only in the public slotreceiveLimit
the value fromlimit
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