Changing values of a QMap variable
-
I've entries as below
Name:
Place:Info
stores them in the form ofconst QMap<QString, QString> &info
There's an entry
Name: Jaret
whose name has to be changed toJane
I've written following code to do the desired change :
QString req_name = info.value("Name"); if (req_name == "Jaret") { req_name = "Jane"; info["Name"] = req_name ; // This line gives error as --> no viable overload '=' }
Can someone suggest me how to do it?
-
@Swati777999 Please post whole error message.
-
@Swati777999 so....
how to you expect to change the content of a const reference object?
const QMap<QString, QString> &info
-
@Swati777999 said in Changing values of a QMap variable:
Info stores them in the form of
const QMap<QString, QString> &info
You cannot change the content of a const value!! This is C/C++ basic.
[EDIT] @J-Hilk was faster ;)
-
@J-Hilk
Yes, you got it right!
Actually ,I've an entire code written by someone else, I am fixing some errors and introducing changes.I tried solving it without
const
but the result which appears now is even horrible.Any alternative would you like to suggest me for it?
-
@Swati777999 said in Changing values of a QMap variable:
Any alternative would you like to suggest me for it?
Can you please explain what exactly happens if you remove const?
-
@Swati777999 said in Changing values of a QMap variable:
Any alternative would you like to suggest me for it?
It is hard to give you alternatives, because it is not clear (to me at least) what you want to do.
And to be totally honest, I don't really understand what kind of change you have done and what the "horrible" result is. -
@Swati777999 This is not enough to give you any concrete solution.
You need to debug the application to see what happens if info is not const. Maybe info does not contain anything? -
In the following program taken from tutorialspoint , the value of const. is changed.
#include <iostream> using namespace std; int main() { const int a = 20; const int* b = &a; cout<<"old value is"<<*b<<"\n"; int* c=const_cast<int *>(b); *c=40; cout<<"new value is"<<*b; return 0; }
It does not seem to be working in the case of QMap [replacing
int
withQMap
]. Can someone give some suggestions? -
@Swati777999 said in Changing values of a QMap variable:
the value of const. is changed
Yes, you can do dirty tricks like casting away const. But you should really avoid this!
You should rather try to understand what the code is doing and what you need to change. Else you will write spagetti code nobody understands and which breaks easilly! -
@jsulm said in Changing values of a QMap variable:
@Swati777999 said in Changing values of a QMap variable:
the value of const. is changed
Yes, you can do dirty tricks like casting away const. But you should really avoid this!
You should rather try to understand what the code is doing and what you need to change. Else you will write spagetti code nobody understands and which breaks easily!The above code seems not to be working with Qt Classes which has amplified my anxiety. I have already spent a day figuring out what to do with const. , now, it feels another day will go in search of a solution! :'(
-
@Swati777999 said in Changing values of a QMap variable:
The above code seems not to be working with Qt Classes which has amplified my anxiety. I have already spent a day figuring out what to do with const.
If you don't know what
const
is/does stay miles away fromconst_cast
. As far as you are concerned it does not exist.If you give us a snippet of code we can work on we might help but at this stage it's impossible for us to assist
-
@Swati777999 you have to be very careful wenn using const_cast, it has very limited legit use cases and most cases people use it for is actually undefined behaviour!
-
@VRonin said in Changing values of a QMap variable:
@Swati777999 said in Changing values of a QMap variable:
The above code seems not to be working with Qt Classes which has amplified my anxiety. I have already spent a day figuring out what to do with const.
If you don't know what
const
is/does stay miles away fromconst_cast
. As far as you are concerned it does not exist.If you give us a snippet of code we can work on we might help but at this stage it's impossible for us to assist
Info
is a QMap with entriesName
&Place
in it.void MainWindow::onConnect(const QMap<QString, QString> &info) { QString info_name = Info["Name"]; qDebug()<< "Name before change="<<info_name; // output : Jaret if (info_name == "Jaret") { model_name = "Jane"; qDebug()<< "Now Name after change="<<info_name; // output : Jane //Info["Name"]=info_name; // Error occurs here }
-
@Swati777999 said in Changing values of a QMap variable:
void MainWindow::onConnect(const QMap<QString, QString> &info)
It should be clear (C++ basics) that if you want to change info inside that method you have to make it non-const:
void MainWindow::onConnect(QMap<QString, QString> &info)
-
@VRonin said in Changing values of a QMap variable:
@jsulm said in Changing values of a QMap variable:
void MainWindow::onConnect(QMap<QString, QString> &info)
Yeah but it looks like a slot so this is probably not the right way to go about changing its contents
If I make it non-const, then it is giving unrelated results. So, I tried for const_cast conversion, and that too seemed not to be working with Qt classes. Any suggestions that you would like to give?
-
Hi
You must change both .cpp and .h to
for
void onConnect(QMap<QString, QString> &info)That is - to remove the const so you can modify "info" in the method.
Both places.Then this syntax will work
Info["Name"]=info_name;Fooling around with const_cast wont bring anything good.
Simply just allow "info" to be changed removing the const is the way to go :=)
and maybe you need to fix up some signal if used with it and it also list info as const.