Results 1 to 6 of 6

Thread: keyPressEvent() reimplementation

  1. #1
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default keyPressEvent() reimplementation

    Hi!

    I wanted to ask, if i reimplenet keyPressEvent() and I just want to change the operation of a single key event, for example space, can I do it only for this key?

    I tried to do it, but if I don't assign tasks for the other keys as well, they don't work. Isn't there a default task for each key for a specific widget, such as TextEdit ?

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: keyPressEvent() reimplementation

    I need to see your code, but it sounds like you need to pass the keyPressEvent() up to the base class, so something like this:

    Qt Code:
    1. MyWidget::keyPressEvent(QKeyEvent * ke)
    2. {
    3. if(ke->key() == Qt::Key_Return)
    4. {
    5. //do something
    6. }
    7.  
    8. //now pass the keyEvent up to the parent widget,
    9. //here I'm assuming its a QWidget
    10. QWidget::keyPressEvent(ke);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 30th October 2008 at 00:49.

  3. The following user says thank you to JimDaniel for this useful post:

    aurelius (30th October 2008)

  4. #3
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: keyPressEvent() reimplementation

    Yes, I am doing exactly this. There is no point pasting my code here, it's like yours.

    What I am trying to do is, to create sth like a console, like a linux console.
    So, in my main window, I have a new class that inherits QTextEdit, e.g QConsole.

    I reimplement keyPressEvent() function for QConsole as you told me, so I do QConsole::keyPressEvent(){...what you wrote...}. When I add the if statement for Qt::Key_Return, it works perfect. Also, when I add some other key stroke event, it works perfect. Howeve if I don't add them, key strokes don't take their default use.

    For example, if I add only Key_Return if statement, the window responds only when I press the return key. All the other don't do anything. When I reimplement keyPressEvent, the other keys don't keep their usual functionality? Is there any way to make them to keep their functionality and change only what I want?

    Finally, I don;t see, why I should pass it to some parent widget. The parent widget of the QConsole is just a widget, but ok, I can't see the relation. The thing is tha keyPressEvent suffers.

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: keyPressEvent() reimplementation

    did you try to use
    Qt Code:
    1. QTextEdit::keyPressEvent(ke);
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. QWidget::keyPressEvent(ke);
    To copy to clipboard, switch view to plain text mode 
    ?

  6. The following user says thank you to spirit for this useful post:

    aurelius (30th October 2008)

  7. #5
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: keyPressEvent() reimplementation

    Ok, guys I found it. I just understood what you meant.

    I was actually confused, because when you said that I have to pass it to the base class, for some stupid reason, I thought you meant as base class the widget in which I have as a child my class.

    Thank you for your help.

  8. #6
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: keyPressEvent() reimplementation

    My apologies, I made a mistake referring to the "parent" widget in my comment, when I really meant the base class. A parent is of course something entirely different.

Similar Threads

  1. Replies: 6
    Last Post: 13th May 2008, 13:19
  2. QTreeWidget keyPressEvent
    By hgedek in forum Qt Programming
    Replies: 3
    Last Post: 18th August 2007, 16:33
  3. F2 keyPressEvent Not Capturing
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 2nd August 2007, 20:41
  4. Handling of dead keys in keyPressEvent()
    By ghorwin in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2006, 12:26
  5. KeyPressEvent behaves strange, skips the F key.
    By pir in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2006, 17:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.