Solved changing a value in a const funktion
-
@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 -
@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; }
?
-
Thank you very much, that was it
I had a second instance of tableCustomise...@J-Hilk
@Christian-Ehrlicher
@VRonin
@JonB
Also thank you, toosorry for my bad explenation
have a nice day -
@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 :-)