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. QPushButton's shortcut not work when the text change
Forum Updated to NodeBB v4.3 + New Features

QPushButton's shortcut not work when the text change

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 5.5k Views 3 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.
  • S SGaist
    16 Feb 2016, 11:32

    That's the correct behavior following the text property

    L Offline
    L Offline
    ljc123456gogo
    wrote on 16 Feb 2016, 11:36 last edited by
    #7

    @SGaist
    Oh! I see:

    Any previous shortcut will be overwritten or cleared if no shortcut is defined by the text.

    This property is really easy to make puzzle.
    Maybe I should reset the shortcut after the text change.
    Thanks.

    Keep Hungery, Keep Foolish!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Feb 2016, 12:44 last edited by
      #8

      If you want to keep the same shortcut then yes.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L ljc123456gogo
        16 Feb 2016, 11:29

        @ValentinMichelet

        Thanks reply.

        1. The text is assigned by user, so I don't know how many count of the Button name, so I use string list for the uniformity.
        2. I know it will use copy construct, but the argument is temp variable , so I can't use reference.
        3. Okay. I add QKeySequence to contain it now.

        You give me a tip to debug the shortcut, I even not call to mind before.
        And I try to qDebug:

        void CustomButton::ChangeNextText()
        {
        	if (m_NameList.length() > 1)
        	{
        		m_NameList.push_back(m_NameList.front());
        		m_NameList.pop_front();
        		qDebug() << "before:" << shortcut();
        		setText(m_NameList[0]);
        		qDebug() << "later:" << shortcut();
        	}
        }
        

        The amazing things is that the setText() method let the Shortcut doesn't work anymore in fact:

        before: QKeySequence("Shift+T")
        later: QKeySequence("")

        It maybe impossible is the m_NameList[0] wrong, because I change m_NameList[0] to anyother string also will happen this question.
        Isn't Qt Bug? Or have other something wrong?

        V Offline
        V Offline
        ValentinMichelet
        wrote on 16 Feb 2016, 13:48 last edited by ValentinMichelet
        #9

        @ljc123456gogo said:

        1. I know it will use copy construct, but the argument is temp variable , so I can't use reference.

        I don't get it. Did you assume that the compiler would complain, or did you actually get a compilation error with a const& variable? In fact I'm not sure to understand what you mean by "temp variable".

        class A {
        public:
          void methodA(QString const& p_string) {
            /* do stuff with p_string, even assign it to a private member... */
          }
        };
        
        int main(int argc, char *argv[])
        {
          A a;
          QString qstr("My string");
          a.methodA(qstr);
        
          return 0;
        }
        

        This works perfectly fine, and I think that qstr is a temp variable, right?

        Anyway, I didn't know that calling setText() would remove the previous shortcut. This is so unintuitive. Does anyone know the reason behind this strange behavior?

        L 1 Reply Last reply 16 Feb 2016, 14:01
        0
        • V ValentinMichelet
          16 Feb 2016, 13:48

          @ljc123456gogo said:

          1. I know it will use copy construct, but the argument is temp variable , so I can't use reference.

          I don't get it. Did you assume that the compiler would complain, or did you actually get a compilation error with a const& variable? In fact I'm not sure to understand what you mean by "temp variable".

          class A {
          public:
            void methodA(QString const& p_string) {
              /* do stuff with p_string, even assign it to a private member... */
            }
          };
          
          int main(int argc, char *argv[])
          {
            A a;
            QString qstr("My string");
            a.methodA(qstr);
          
            return 0;
          }
          

          This works perfectly fine, and I think that qstr is a temp variable, right?

          Anyway, I didn't know that calling setText() would remove the previous shortcut. This is so unintuitive. Does anyone know the reason behind this strange behavior?

          L Offline
          L Offline
          ljc123456gogo
          wrote on 16 Feb 2016, 14:01 last edited by
          #10

          @ValentinMichelet
          I said temp variable that means like:

          int main(int argc, char *argv[])
          {
            A a;
            a.methodA(QString("My string")); //When the fuction call end, this QString was "dead", it's memory maybe change by other pragram
          
            return 0;
          }
          

          And when the QString getout of it's scope, it also will "dead". It belong C++ knowledge.
          Your code run normally because the QString until the pragram end it still exist, it is not what I mean.

          Keep Hungery, Keep Foolish!

          1 Reply Last reply
          0
          • V Offline
            V Offline
            ValentinMichelet
            wrote on 16 Feb 2016, 14:20 last edited by ValentinMichelet
            #11

            I still don't get it, sorry.

            #include <QDebug>
            
            class A {
            public:
              void setString(QString const& p_string) {
                m_string = p_string;
              }
              QString m_string;
            };
            
            int main(int argc, char *argv[])
            {
              A a;
              a.setString(QString("My string"));
              qDebug() << a.m_string;
            
              return 0;
            }
            

            This works like a charm.

            L 2 Replies Last reply 16 Feb 2016, 14:42
            0
            • V ValentinMichelet
              16 Feb 2016, 14:20

              I still don't get it, sorry.

              #include <QDebug>
              
              class A {
              public:
                void setString(QString const& p_string) {
                  m_string = p_string;
                }
                QString m_string;
              };
              
              int main(int argc, char *argv[])
              {
                A a;
                a.setString(QString("My string"));
                qDebug() << a.m_string;
              
                return 0;
              }
              

              This works like a charm.

              L Offline
              L Offline
              ljc123456gogo
              wrote on 16 Feb 2016, 14:42 last edited by ljc123456gogo
              #12

              @ValentinMichelet
              I say: it's memory maybe changed by other pragram later. Not immediate be changed. It would not have complie error. If you want to see error, you can let it wait some minutes and see it's data, it maybe changed.

              Keep Hungery, Keep Foolish!

              1 Reply Last reply
              0
              • V ValentinMichelet
                16 Feb 2016, 14:20

                I still don't get it, sorry.

                #include <QDebug>
                
                class A {
                public:
                  void setString(QString const& p_string) {
                    m_string = p_string;
                  }
                  QString m_string;
                };
                
                int main(int argc, char *argv[])
                {
                  A a;
                  a.setString(QString("My string"));
                  qDebug() << a.m_string;
                
                  return 0;
                }
                

                This works like a charm.

                L Offline
                L Offline
                ljc123456gogo
                wrote on 16 Feb 2016, 14:52 last edited by
                #13

                @ValentinMichelet
                you also can see http://stackoverflow.com/questions/10540157/c-temporary-variable-lifetime

                Keep Hungery, Keep Foolish!

                V 1 Reply Last reply 16 Feb 2016, 15:16
                0
                • L ljc123456gogo
                  16 Feb 2016, 14:52

                  @ValentinMichelet
                  you also can see http://stackoverflow.com/questions/10540157/c-temporary-variable-lifetime

                  V Offline
                  V Offline
                  ValentinMichelet
                  wrote on 16 Feb 2016, 15:16 last edited by ValentinMichelet
                  #14

                  @ljc123456gogo said:

                  @ValentinMichelet
                  you also can see http://stackoverflow.com/questions/10540157/c-temporary-variable-lifetime

                  This is a completely different issue. First, the reference is constant in my code, not in your link. Then, it's a parameter, not something you create inside your constructor.
                  The code I provided is a classic way to call a method with some parameters that you won't modify (read only) which insures that no copy will be done. There won't be any issue if you simply read it. It won't be overwritten when you are inside of your constructor. This is C++ knowledge.

                  Two problems can occur tho:

                  1. You const_cast it and modify it, but whatever happens, you deserve it.
                  2. You store the address of the temp, leading to a potential segfault.

                  Other than that, there is nothing wrong. In fact, it's a good practice to pass a const ref in a method/constructor as long as you do not have to modify it inside your method/constructor.

                  L 1 Reply Last reply 16 Feb 2016, 15:28
                  1
                  • V ValentinMichelet
                    16 Feb 2016, 15:16

                    @ljc123456gogo said:

                    @ValentinMichelet
                    you also can see http://stackoverflow.com/questions/10540157/c-temporary-variable-lifetime

                    This is a completely different issue. First, the reference is constant in my code, not in your link. Then, it's a parameter, not something you create inside your constructor.
                    The code I provided is a classic way to call a method with some parameters that you won't modify (read only) which insures that no copy will be done. There won't be any issue if you simply read it. It won't be overwritten when you are inside of your constructor. This is C++ knowledge.

                    Two problems can occur tho:

                    1. You const_cast it and modify it, but whatever happens, you deserve it.
                    2. You store the address of the temp, leading to a potential segfault.

                    Other than that, there is nothing wrong. In fact, it's a good practice to pass a const ref in a method/constructor as long as you do not have to modify it inside your method/constructor.

                    L Offline
                    L Offline
                    ljc123456gogo
                    wrote on 16 Feb 2016, 15:28 last edited by
                    #15

                    @ValentinMichelet
                    Okay, I make some misunderstand, I overlook the "const" in your codes before. If doesn't have const, it's danger.
                    I should use const reference.

                    Keep Hungery, Keep Foolish!

                    V 1 Reply Last reply 16 Feb 2016, 15:32
                    1
                    • L ljc123456gogo
                      16 Feb 2016, 15:28

                      @ValentinMichelet
                      Okay, I make some misunderstand, I overlook the "const" in your codes before. If doesn't have const, it's danger.
                      I should use const reference.

                      V Offline
                      V Offline
                      ValentinMichelet
                      wrote on 16 Feb 2016, 15:32 last edited by
                      #16

                      @ljc123456gogo
                      I'm glad we both agree on that =)

                      1 Reply Last reply
                      0

                      16/16

                      16 Feb 2016, 15:32

                      • Login

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