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
Forum Updated to NodeBB v4.3 + New Features

Changing values of a QMap variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 5.2k 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 Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    I've entries as below
    Name:
    Place:

    Info stores them in the form of const QMap<QString, QString> &info

    There's an entry Name: Jaret whose name has to be changed to Jane

    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?

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

    jsulmJ J.HilkJ KroMignonK 3 Replies Last reply
    0
    • Swati777999S Swati777999

      I've entries as below
      Name:
      Place:

      Info stores them in the form of const QMap<QString, QString> &info

      There's an entry Name: Jaret whose name has to be changed to Jane

      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?

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

      @Swati777999 Please post whole error message.

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

      1 Reply Last reply
      0
      • Swati777999S Swati777999

        I've entries as below
        Name:
        Place:

        Info stores them in the form of const QMap<QString, QString> &info

        There's an entry Name: Jaret whose name has to be changed to Jane

        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?

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

        @Swati777999 so....

        how to you expect to change the content of a const reference object?

        const QMap<QString, QString> &info


        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.

        Swati777999S 1 Reply Last reply
        5
        • Swati777999S Swati777999

          I've entries as below
          Name:
          Place:

          Info stores them in the form of const QMap<QString, QString> &info

          There's an entry Name: Jaret whose name has to be changed to Jane

          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?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

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

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          5
          • J.HilkJ J.Hilk

            @Swati777999 so....

            how to you expect to change the content of a const reference object?

            const QMap<QString, QString> &info

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

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

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

            jsulmJ KroMignonK 2 Replies Last reply
            0
            • Swati777999S Swati777999

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

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

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

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

              Swati777999S 1 Reply Last reply
              1
              • Swati777999S Swati777999

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

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

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

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • jsulmJ jsulm

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

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

                  @jsulm

                  There's a display of some connected devices on the main window when const is present. If I remove const, this list doesn't appear in the main window. That's all I can say , at this point of time.

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

                  jsulmJ 1 Reply Last reply
                  0
                  • Swati777999S Swati777999

                    @jsulm

                    There's a display of some connected devices on the main window when const is present. If I remove const, this list doesn't appear in the main window. That's all I can say , at this point of time.

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

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

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

                    Swati777999S 1 Reply Last reply
                    1
                    • jsulmJ jsulm

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

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

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

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

                      jsulmJ 1 Reply Last reply
                      0
                      • 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

                                          • Login

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