Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing values of a QMap variable
Qt 6.11 is out! See what's new in the release blog

Changing values of a QMap variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Swati777999S Swati777999

    @jsulm

    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 with QMap]. Can someone give some suggestions?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @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!

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Swati777999S 1 Reply Last reply
    2
    • jsulmJ jsulm

      @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!

      Swati777999S Offline
      Swati777999S Offline
      Swati777999
      wrote on last edited by
      #12

      @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! :'(

      “ In order to be irreplaceable, one must always be different” – Coco Chanel

      VRoninV J.HilkJ 2 Replies Last reply
      0
      • Swati777999S Swati777999

        @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! :'(

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #13

        @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 from const_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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        Swati777999S 1 Reply Last reply
        3
        • Swati777999S Swati777999

          @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! :'(

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #14

          @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!


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • VRoninV VRonin

            @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 from const_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

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #15

            @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 from const_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 entries Name & 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
               }
            

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            jsulmJ 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #16

              Here info is passed as a read only. You can consider it even a temporary if you want but it stops existing at the end of the function so changing it makes little sense.

              where does info come from? is it a member of some other class?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • Swati777999S Swati777999

                @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 from const_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 entries Name & 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
                   }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #17

                @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)
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                VRoninV Swati777999S 2 Replies Last reply
                1
                • jsulmJ jsulm

                  @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)
                  
                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #18

                  @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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  Swati777999S 1 Reply Last reply
                  3
                  • VRoninV VRonin

                    @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

                    Swati777999S Offline
                    Swati777999S Offline
                    Swati777999
                    wrote on last edited by
                    #19

                    @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?

                    “ In order to be irreplaceable, one must always be different” – Coco Chanel

                    mrjjM 1 Reply Last reply
                    0
                    • Swati777999S Swati777999

                      @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?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #20

                      @Swati777999

                      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.

                      Swati777999S 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @Swati777999

                        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.

                        Swati777999S Offline
                        Swati777999S Offline
                        Swati777999
                        wrote on last edited by
                        #21

                        @mrjj said in Changing values of a QMap variable:

                        @Swati777999

                        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 signal if 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.

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @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)
                          
                          Swati777999S Offline
                          Swati777999S Offline
                          Swati777999
                          wrote on last edited by
                          #22

                          @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.

                          “ In order to be irreplaceable, one must always be different” – Coco Chanel

                          jsulmJ 1 Reply Last reply
                          0
                          • Swati777999S Swati777999

                            @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.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #23

                            @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.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved