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 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.
-
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.
@mrjj said in Changing values of a QMap variable:
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 :=)It is not working. I've already tried it in this way and it is showing the undesired result as discussed in my previous comments. It's the first thing that I tried. This is a slot function used twice in my program as private and public slot.
and maybe you need to
fix up some signalif used with it and it also list info as const.Thanks for this suggestion. This had not struck me before. I will try it this way.
-
@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:
@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)Tried it in both header and source file and the result comes out to be undesirable.
-
@jsulm said in Changing values of a QMap variable:
@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)Tried it in both header and source file and the result comes out to be undesirable.
@Swati777999 @VRonin asked you where info comes from: can you please answer this question? It sounds somewhat strange if a slot changes its parameters content.