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. [SOLVED] Subclassing QLineEdit and typing a special character when a key combination is pressed
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Subclassing QLineEdit and typing a special character when a key combination is pressed

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.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.
  • O Offline
    O Offline
    OwonaVivienD
    wrote on last edited by
    #1

    Good afternoon.

    I'm trying to subclass QLineEdit because I need to modify its behaviour pattern for few key combinations like Shift+Space and Shift+Return. I actually want it to type some special characters when one of these key combinations is pressed. For instance, when I press Shift+Space, I want my new LineEdit to type "■" (U+25A0) and, when I press Shift+Return, I want it to type "¶" (U+00B6).

    My problem is I don't know which protected function I should modify or how I should modify it. Is it keyPressEvent, inputMethodEvent or another function? If I have to use or modify inputMethodeEvent, how should I use the class QInputMethodEvent?

    Thank you in advance for your help.

    By the way, "Vivien" is a male name in French...

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      this should do it:
      @
      void MyLineEdit::keyPressEvent(QKeyEvent* event)
      {
      if( event->modifiers() & Qt::ShiftModifier )
      {
      switch( event->key() )
      {
      case Qt::Key_Space:
      this->insert( QString::fromUtf8("\u25A0") );
      return;
      break;
      case Qt::Key_Return:
      case Qt::Key_Enter:
      this->insert( QString::fromUtf8("\u00B6") );
      return;
      break;
      }
      }

      QLineEdit::keyPressEvent(event);
      

      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        raven-worx: don't forget to check for the Shift key modifier though...

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          [quote author="Andre" date="1375189703"]raven-worx: don't forget to check for the Shift key modifier though...[/quote]
          i did...first line in the event handler ;)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="raven-worx" date="1375189870"][quote author="Andre" date="1375189703"]raven-worx: don't forget to check for the Shift key modifier though...[/quote]
            i did...first line in the event handler ;)
            [/quote]

            Doh! Sorry, you are right of course.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              monsingh
              wrote on last edited by
              #6

              void MyLineEdit::keyPressEvent(QKeyEvent *event)

              keyPressEvent is enough to control all key press event in qt.

              1 Reply Last reply
              0
              • O Offline
                O Offline
                OwonaVivienD
                wrote on last edited by
                #7

                That works well. Thank you for your help.

                PS: What do the function inputMethodEvent actually do? When should I modify it?

                By the way, "Vivien" is a male name in French...

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  basically you don't need to use inputMethodEvent handler if you don't know what it is doing ... i know stupid answer ;)
                  I never had to use it so far. It's basically used if a special QInputContext is installed on the QApplication. So if you use Qt as it is, you don't need to use it.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  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