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. Preventing key press in a text edit
Forum Updated to NodeBB v4.3 + New Features

Preventing key press in a text edit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.6k 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.
  • E Offline
    E Offline
    ecir.hana
    wrote on last edited by
    #1

    Hello,

    I have a simple application which host one QPlainTextEdit, I can type into it easily. But when I press a key with additional key modifier (e.g. cmd+m), the key appears in the edit, too.

    I other words, if I hit "m", "m" appears. If I hit "cmd+m", "m" appears.

    Please, how to prevent the key press if there was present a modifier? I would like "cmd+m" to do nothing unless there is a QAction defined. In other words, "cmd+z" should undo because there is undo defined, "cmd+n" should open new window if the is a QAction with "cmd+n" shortcut. But if there is no such action with shortcut I don't want the key to appear in the edit.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bodzio131
      wrote on last edited by
      #2

      What about re-implementing QPlainTextEdit::keyPressEvent and block modified key events (check QKeyEvent::modifiers method)?

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ecir.hana
        wrote on last edited by
        #3

        Thanks, that would probably work. But the thing is, Qt Creator does exhibit the wanted behavior described above and I cannot find how they do it. There is no checking of modifiers for ctrl in "basetexteditor.cpp" so I'm wondering whether there is other way of doing this..?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bodzio131
          wrote on last edited by
          #4

          Well, definitely I have different basetexteditor.cpp file. For example this line in BaseTextEditorWidget::keyPressEvent (taken from http://qt.gitorious.org ):
          @
          } else if ((e->modifiers() & (Qt::ControlModifier|Qt::AltModifier)) != Qt::ControlModifier){
          @

          seems to block ctrl-modified key events. And there is several other places in this file where 'control' modifier, or key, is checked.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ecir.hana
            wrote on last edited by
            #5

            Right you are! Stupid me, I was looking for "ctrl" and not "ControlModifier". Thank you very much!

            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