Changing values of a QMap variable
-
I've entries as below
Name:
Place:Infostores them in the form ofconst QMap<QString, QString> &infoThere's an entry
Name: Jaretwhose name has to be changed toJaneI'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?
-
I've entries as below
Name:
Place:Infostores them in the form ofconst QMap<QString, QString> &infoThere's an entry
Name: Jaretwhose name has to be changed toJaneI'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.
-
I've entries as below
Name:
Place:Infostores them in the form ofconst QMap<QString, QString> &infoThere's an entry
Name: Jaretwhose name has to be changed toJaneI'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 so....
how to you expect to change the content of a const reference object?
const QMap<QString, QString> &info -
I've entries as below
Name:
Place:Infostores them in the form ofconst QMap<QString, QString> &infoThere's an entry
Name: Jaretwhose name has to be changed toJaneI'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 said in Changing values of a QMap variable:
Info stores them in the form of
const QMap<QString, QString> &infoYou cannot change the content of a const value!! This is C/C++ basic.
[EDIT] @J-Hilk was faster ;)
-
@Swati777999 so....
how to you expect to change the content of a const reference object?
const QMap<QString, QString> &info@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
constbut the result which appears now is even horrible.Any alternative would you like to suggest me for it?
-
@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
constbut 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?
-
@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
constbut 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?
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 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?
There's a display of some connected devices on the main window when
constis present. If I removeconst, this list doesn't appear in the main window. That's all I can say , at this point of time. -
There's a display of some connected devices on the main window when
constis present. If I removeconst, this list doesn't appear in the main window. That's all I can say , at this point of time.@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? -
@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
intwithQMap]. Can someone give some suggestions? -
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
intwithQMap]. 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! -
@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! :'(
-
@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
constis/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
-
@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 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!
-
@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
constis/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
@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
constis/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
Infois a QMap with entriesName&Placein 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 } -
@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
constis/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
Infois a QMap with entriesName&Placein 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) -
@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) -
@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
@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?
-
@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.