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. Handling shortcut() in keyPressEvent

Handling shortcut() in keyPressEvent

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 6.4k 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.
  • L Offline
    L Offline
    lemzwerg
    wrote on last edited by
    #1

    I want to implement a WarningCheckBox class, derived from QCheckBox, which pops up a warning message if I click on the unchecked box, asking for confirmation. If the result is `yes', continue, otherwise do nothing.

    Searching for Qt demo code which does something similar yielded no results, unfortunately, so I tried by myself, and I'm stuck somehow to find a satisfying solution.

    It seems to me that I have to intercept all events which eventually lead to a checkbox toggle: mousePressEvent and keyPressEvent; the latter should take care of both the space key (which toggles the checkbox by default if it has the focus) and a shortcut.

    [What I actually want is a hook which gets called right before QCheckBox is doing the normal stuff for toggling its state; doing so would avoid fiddling with the events directly, but apparently this doesn't exist.]

    Handling the mouse click and the space key is trivial. However, what I have problems with is the handling of the shortcut. Here my naïve implementation which fails. [The function `havePermission' pops up the warning message box.]

    @
    void
    WarningCheckBox::keyPressEvent(QKeyEvent* event)
    {
    if (isChecked()
    || (((hasFocus() && event->key() == Qt::Key_Space)
    || event == shortcut())
    && havePermission()))
    QCheckBox::keyPressEvent(event);
    }
    @

    The compiler returns an error for `event == shortcut()'

    error: invalid conversion from 'int' to 'QKeySequence::StandardKey'

    /usr/include/QtGui/qevent.h:736:13: error: initializing argument 2 of
    'bool operator==(QKeyEvent*, QKeySequence::StandardKey)'

    So my question is: How do I do this correctly?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lemzwerg
      wrote on last edited by
      #2

      I take back the trivial' word. Even the mouse stuff doesn't work as expected: Somehow events seem to get lost. For example, I have to click the check button twice (and say twice yes' in the pop-up warning message) until I see a check. It is very obvious that I try things which I don't understand.

      Any help would be greatly appriciated.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Can you make some prototype ui of what your dialog should look (eg. using Qt Designer). You can upload the image to an image hoster of your choice (DevNet doesn't allow image uploads).

        Also, could you rephrase the description of what that dialog should actually do? I didn't understand it completely.

        Additionally, you may want to look at [[Doc:QErrorMessage]], maybe it is of use for you. But this is just a wild guess :)

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lemzwerg
          wrote on last edited by
          #4

          Well, it's not a layout problem, it's an event (or signal?) communication problem within Qt which I don't manage correctly:

          I want a simple check box. If it is unchecked, and I click' on it (either with the mouse or the keyboard), a QMessageDialog should pop up, with Yes' and No'. If I say Yes', the dialog should disappear, as usual, and the check mark should be set.
          If I say no, the check mark should not be set.

          Nothing more.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You should re-think your UI design in general - seriously.

            A user expects a checkbox to be checkable directly not via a popup dialog. This is the well expected behavior on all platforms that I ever came across.

            Don't change that behavior unless you have very, very good reasons - and to be honest I doubt, that you have (as with almost all similar UI behavior changing requests that pop up here in the forums).

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lemzwerg
              wrote on last edited by
              #6

              I've meanwhile changed the design :-)

              However, there was a very good reasoning for having this warning checkbox: It was intended to activate a feature for which you need a legal permission.

              Independently, I would like to know how to implement such a feature, regardless of the UI quality so that I can understand the Qt mechanisms better.

              And thanks for you help!

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                Two possible approaches come into mind:

                • disable the checkbox if the user is not allowed to check it;use "QWidget::setEnabled(false) ":/doc/qt-4.8/qwidget.html#enabled-prop
                • connect to [[Doc:QCheckBox]]'s "stateChanged":/doc/qt-4.8/qcheckbox.html#stateChanged signal and have the slot popup the dialog

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lemzwerg
                  wrote on last edited by
                  #8

                  Thanks a lot for your patience!

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lemzwerg
                    wrote on last edited by
                    #9

                    BTW, I've just found a real example of what I originally wanted to do. Under Windows, you can adjust the behaviour of how files are displayed. For example, there is a checkbox to make Windows show hidden files in folders. If you click on that checkbox, a message window pops up, asking for confirmation. Here you can see it with images:

                    http://www.sevenforums.com/tutorials/394-hidden-files-folders-show-hide.html

                    It seems that my original idea is not that a bad GUI behaviour...

                    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