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 in QMessageBox missing key shortcut underline on initial display
QtWS25 Last Chance

QPushButton in QMessageBox missing key shortcut underline on initial display

Scheduled Pinned Locked Moved Solved General and Desktop
qpushbuttonqmessageboxshortcutunderline
24 Posts 4 Posters 13.6k Views
  • 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.
  • H Offline
    H Offline
    Harry123
    wrote on 9 Mar 2016, 12:06 last edited by Harry123 3 Sept 2016, 12:22
    #1

    I'm under Windows, and I add several QPushButtons to a QMessageBox. A shortcut key is specified by an ampersand before the preferred character in each text.

    When the message box is initially displayed, the shortcut keys of all buttons are not marked by an underline, but :

    1. The button does execute if I type the shortcut key in spite of the missing underline.
    2. If I press the Alt key, the shortcut underline appears on all buttons and stays displayed after I release the Alt key.

    This lack of an initial visual cue may cause the end-user to assume that shortcuts were not provided.

    Is there something I should do to force the display of the shortcut underlines when the message box is initially displayed ?

    H J 2 Replies Last reply 9 Mar 2016, 15:41
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Mar 2016, 12:20 last edited by mrjj 3 Sept 2016, 12:32
      #2

      Hi
      I think its default on windows NOT to show underscore.
      At least all other new programs I have - does this.
      This is win 7.

      This settings seems to be controlled via
      Open Control Panel / Ease of Access Center / Make the Keyboard easier to use.
      Underline keyboard shortcuts and access keys.

      H 1 Reply Last reply 9 Mar 2016, 12:32
      0
      • M mrjj
        9 Mar 2016, 12:20

        Hi
        I think its default on windows NOT to show underscore.
        At least all other new programs I have - does this.
        This is win 7.

        This settings seems to be controlled via
        Open Control Panel / Ease of Access Center / Make the Keyboard easier to use.
        Underline keyboard shortcuts and access keys.

        H Offline
        H Offline
        Harry123
        wrote on 9 Mar 2016, 12:32 last edited by
        #3

        @mrjj

        I don't know if there is a default. Most programs display underlines in dialogs but some don't.
        For example wordpad and notepad and Word do have them on the "save before exit" dialog (Windows 7).
        It might be something to do with how one calls the Windows API.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 9 Mar 2016, 12:33 last edited by mrjj 3 Sept 2016, 12:33
          #4

          it is.
          found setting.

          This settings seems to be controlled via
          Open Control Panel / Ease of Access Center / Make the Keyboard easier to use.
          Underline keyboard shortcuts and access keys.

          H 1 Reply Last reply 9 Mar 2016, 12:54
          1
          • M mrjj
            9 Mar 2016, 12:33

            it is.
            found setting.

            This settings seems to be controlled via
            Open Control Panel / Ease of Access Center / Make the Keyboard easier to use.
            Underline keyboard shortcuts and access keys.

            H Offline
            H Offline
            Harry123
            wrote on 9 Mar 2016, 12:54 last edited by
            #5

            @mrjj

            You are right that this setting forces the initial display of the underlines in QMessageBox buttons.
            However, for most programs underlines are displayed with or without this setting.

            Without this setting, I have tried out several program of mine that display dialogs, and find that some display underlines and some don't.

            Weirder and weirder.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Mar 2016, 13:05 last edited by
              #6

              well My word 2010 did change for fileopen dialog.
              Also my notepad++.

              But there must be a way to show always in windows API as
              some of my programs always show it. Even same buttons.

              http://stackoverflow.com/questions/14054036/show-hotkeys-at-all-times
              here they use sendmessage to turn it on. Should also be possible in
              Qt if no Qt way exists.

              H 1 Reply Last reply 9 Mar 2016, 14:12
              0
              • M mrjj
                9 Mar 2016, 13:05

                well My word 2010 did change for fileopen dialog.
                Also my notepad++.

                But there must be a way to show always in windows API as
                some of my programs always show it. Even same buttons.

                http://stackoverflow.com/questions/14054036/show-hotkeys-at-all-times
                here they use sendmessage to turn it on. Should also be possible in
                Qt if no Qt way exists.

                H Offline
                H Offline
                Harry123
                wrote on 9 Mar 2016, 14:12 last edited by
                #7

                @mrjj

                Yes, my thought exactly.

                My code now works and displays the underlines.
                It looks like this :

                QKeyEvent event(QEvent::KeyPress, Qt::Key_Alt, Qt::NoModifier);
                QApplication::sendEvent(msgBox, &event);
                msgBox->exec();
                
                

                However, I would prefer a solution which is less of a kludge, if anyone has a better idea.

                1 Reply Last reply
                2
                • H Harry123
                  9 Mar 2016, 12:06

                  I'm under Windows, and I add several QPushButtons to a QMessageBox. A shortcut key is specified by an ampersand before the preferred character in each text.

                  When the message box is initially displayed, the shortcut keys of all buttons are not marked by an underline, but :

                  1. The button does execute if I type the shortcut key in spite of the missing underline.
                  2. If I press the Alt key, the shortcut underline appears on all buttons and stays displayed after I release the Alt key.

                  This lack of an initial visual cue may cause the end-user to assume that shortcuts were not provided.

                  Is there something I should do to force the display of the shortcut underlines when the message box is initially displayed ?

                  H Offline
                  H Offline
                  Harry123
                  wrote on 9 Mar 2016, 15:41 last edited by Harry123 3 Oct 2016, 15:44
                  #8

                  This is definitely a Qt design decision to by default paint buttons without shortcut underlines.

                  I have searched in the sources and found that QWindowsStyle::eventFilter() in qwindowsstyle.cpp does check for Qt::Key_Alt and then repaints all children, all this after calling an internal function named seenAlt().

                  As it skips all QWidgets having the styleHint of QStyle::SH_UnderlineShortcut, it seems like this is what is missing from my QPushButtons.

                  How do I add this styleHint to my buttons when creating them ?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 9 Mar 2016, 20:37 last edited by
                    #9

                    @Harry123 said:
                    aha. that is good digging around.
                    QStyle::SH_UnderlineShortcut seems to belong to the style used by the application.

                    Im really not sure how to set for a buttons as such. Normally you use a QStyle
                    in paintEvent for a custom button/widget.

                    I guess one would have to provide a custom QStyle that somehow altered
                    this setting.
                    http://doc.qt.io/qt-5.5/qapplication.html#setStyle

                    K 1 Reply Last reply 9 Mar 2016, 22:52
                    0
                    • M mrjj
                      9 Mar 2016, 20:37

                      @Harry123 said:
                      aha. that is good digging around.
                      QStyle::SH_UnderlineShortcut seems to belong to the style used by the application.

                      Im really not sure how to set for a buttons as such. Normally you use a QStyle
                      in paintEvent for a custom button/widget.

                      I guess one would have to provide a custom QStyle that somehow altered
                      this setting.
                      http://doc.qt.io/qt-5.5/qapplication.html#setStyle

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 9 Mar 2016, 22:52 last edited by
                      #10

                      @Harry123
                      Try the qt_set_sequence_auto_mnemonic function.

                      Kind regards.

                      Read and abide by the Qt Code of Conduct

                      H 1 Reply Last reply 10 Mar 2016, 11:57
                      1
                      • K kshegunov
                        9 Mar 2016, 22:52

                        @Harry123
                        Try the qt_set_sequence_auto_mnemonic function.

                        Kind regards.

                        H Offline
                        H Offline
                        Harry123
                        wrote on 10 Mar 2016, 11:57 last edited by
                        #11

                        @kshegunov

                        qt_set_sequence_auto_mnemonic is not found by the linker.
                        In which library is it supposed to be ?
                        I have both Qt5Gui and Qt5Core in the link.

                        K 1 Reply Last reply 10 Mar 2016, 12:22
                        0
                        • H Harry123
                          10 Mar 2016, 11:57

                          @kshegunov

                          qt_set_sequence_auto_mnemonic is not found by the linker.
                          In which library is it supposed to be ?
                          I have both Qt5Gui and Qt5Core in the link.

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 10 Mar 2016, 12:22 last edited by kshegunov 3 Oct 2016, 13:10
                          #12

                          @Harry123
                          My guess would be in the gui module, but I have never used it so I'm not 100% sure. I checked and on my 5.6 build I found it in libQt5Gui.so:

                          0000000000138d02 T _Z29qt_set_sequence_auto_mnemonicb
                          

                          So, the answer is: it is located in the gui module.

                          Read and abide by the Qt Code of Conduct

                          H 1 Reply Last reply 10 Mar 2016, 13:23
                          0
                          • K kshegunov
                            10 Mar 2016, 12:22

                            @Harry123
                            My guess would be in the gui module, but I have never used it so I'm not 100% sure. I checked and on my 5.6 build I found it in libQt5Gui.so:

                            0000000000138d02 T _Z29qt_set_sequence_auto_mnemonicb
                            

                            So, the answer is: it is located in the gui module.

                            H Offline
                            H Offline
                            Harry123
                            wrote on 10 Mar 2016, 13:23 last edited by
                            #13

                            @kshegunov

                            Unfortunately it seems to be missing from the Windows build.

                            K 1 Reply Last reply 10 Mar 2016, 14:44
                            0
                            • H Harry123
                              10 Mar 2016, 13:23

                              @kshegunov

                              Unfortunately it seems to be missing from the Windows build.

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 10 Mar 2016, 14:44 last edited by
                              #14

                              @Harry123
                              Your claim seemed somewhat dubious, so even if I don't actively develop on windows I've loaded the gui module and searched through it with the dependency walker. The symbol is there (Qt 5.5.1 installed with Qt maintenance tool), see the screenshot, and it is exported.
                              qt_set_sequence_auto_mnemonic
                              I don't know what error exactly you're getting but you should be able to use the function if declared properly (as mentioned in the documentation I sourced in my previous post).

                              Read and abide by the Qt Code of Conduct

                              H 1 Reply Last reply 10 Mar 2016, 15:00
                              0
                              • K kshegunov
                                10 Mar 2016, 14:44

                                @Harry123
                                Your claim seemed somewhat dubious, so even if I don't actively develop on windows I've loaded the gui module and searched through it with the dependency walker. The symbol is there (Qt 5.5.1 installed with Qt maintenance tool), see the screenshot, and it is exported.
                                qt_set_sequence_auto_mnemonic
                                I don't know what error exactly you're getting but you should be able to use the function if declared properly (as mentioned in the documentation I sourced in my previous post).

                                H Offline
                                H Offline
                                Harry123
                                wrote on 10 Mar 2016, 15:00 last edited by
                                #15

                                @kshegunov

                                Sorry, my claim was more than dubious - unfortunately I put the call in a section of extern "C".
                                Stupid mistake, but this is a complicated body of code.

                                Results: calling qt_set_sequence_auto_mnemonic with the parameters of either true or false did not restore the underlines.

                                K 1 Reply Last reply 10 Mar 2016, 15:07
                                0
                                • H Harry123
                                  10 Mar 2016, 15:00

                                  @kshegunov

                                  Sorry, my claim was more than dubious - unfortunately I put the call in a section of extern "C".
                                  Stupid mistake, but this is a complicated body of code.

                                  Results: calling qt_set_sequence_auto_mnemonic with the parameters of either true or false did not restore the underlines.

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 10 Mar 2016, 15:07 last edited by
                                  #16

                                  @Harry123

                                  Results: calling qt_set_sequence_auto_mnemonic with the parameters of either true or false did not restore the underlines.

                                  That's unfortunate. Another thing you could try is to set a global proxy style for your application and manually force the QStyle::SH_UnderlineShortcut flag.

                                  Kind regards.

                                  Read and abide by the Qt Code of Conduct

                                  H 1 Reply Last reply 10 Mar 2016, 15:26
                                  1
                                  • K kshegunov
                                    10 Mar 2016, 15:07

                                    @Harry123

                                    Results: calling qt_set_sequence_auto_mnemonic with the parameters of either true or false did not restore the underlines.

                                    That's unfortunate. Another thing you could try is to set a global proxy style for your application and manually force the QStyle::SH_UnderlineShortcut flag.

                                    Kind regards.

                                    H Offline
                                    H Offline
                                    Harry123
                                    wrote on 10 Mar 2016, 15:26 last edited by Harry123 3 Oct 2016, 15:45
                                    #17

                                    @kshegunov

                                    This works. I modified the example in the link with :

                                        if (hint == QStyle::SH_UnderlineShortcut)
                                            return 1;
                                    

                                    I now have underlines. Much better than faking an Alt key and it works for all future dialogs.

                                    Very many thanks.

                                    K 1 Reply Last reply 10 Mar 2016, 17:16
                                    0
                                    • H Harry123
                                      10 Mar 2016, 15:26

                                      @kshegunov

                                      This works. I modified the example in the link with :

                                          if (hint == QStyle::SH_UnderlineShortcut)
                                              return 1;
                                      

                                      I now have underlines. Much better than faking an Alt key and it works for all future dialogs.

                                      Very many thanks.

                                      K Offline
                                      K Offline
                                      kshegunov
                                      Moderators
                                      wrote on 10 Mar 2016, 17:16 last edited by
                                      #18

                                      @Harry123

                                      faking an Alt key

                                      Well, this could also have unforeseeable side effects as well (if for example some widget has a shortcut bound to that key), so it's a bit hackish.

                                      Very many thanks.

                                      You're most welcome.

                                      Read and abide by the Qt Code of Conduct

                                      H 1 Reply Last reply 10 Mar 2016, 20:01
                                      0
                                      • K kshegunov
                                        10 Mar 2016, 17:16

                                        @Harry123

                                        faking an Alt key

                                        Well, this could also have unforeseeable side effects as well (if for example some widget has a shortcut bound to that key), so it's a bit hackish.

                                        Very many thanks.

                                        You're most welcome.

                                        H Offline
                                        H Offline
                                        Harry123
                                        wrote on 10 Mar 2016, 20:01 last edited by Harry123 3 Oct 2016, 20:02
                                        #19

                                        @kshegunov

                                        The only question that is left, is why isn't QStyle::SH_UnderlineShortcut the default .

                                        K 1 Reply Last reply 10 Mar 2016, 20:53
                                        0
                                        • H Harry123
                                          10 Mar 2016, 20:01

                                          @kshegunov

                                          The only question that is left, is why isn't QStyle::SH_UnderlineShortcut the default .

                                          K Offline
                                          K Offline
                                          kshegunov
                                          Moderators
                                          wrote on 10 Mar 2016, 20:53 last edited by
                                          #20

                                          @Harry123
                                          I'd say the default is determined by the OS settings, and Qt simply wouldn't presume to override it. That'd explain why when you change the settings systemwide the behavior changes accordingly.

                                          Read and abide by the Qt Code of Conduct

                                          H 1 Reply Last reply 11 Mar 2016, 09:19
                                          0

                                          10/24

                                          9 Mar 2016, 22:52

                                          • Login

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