Results 1 to 5 of 5

Thread: problem with keypressevent inside textedit

  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy problem with keypressevent inside textedit

    hi,

    im a beginner in programming, im creating a school assignment, such that any keyboard input inside a textedit will be display what type of key entered in the label below.

    void MainWindow::keyPressEvent(QKeyEvent *event){
    if(event->key() == Qt::Key_0 )
    {ui->label->setText("Numbers 0 press");}
    .........
    .........
    else
    ui->label->setText("Other keys press");


    *attached is the source code.

    but the problem is when any key press inside the textedit will not produce any outcome, but if the focus is outside the textedit box (for example by pressing the dummy button) the function will execute.

    i search existing thread but doesnt find anything beneficial..
    i am highly grateful by any help from u guys


    t2.zip

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem with keypressevent inside textedit

    but the problem is when any key press inside the textedit will not produce any outcome,
    That is because you have overridden the keyPressEvent of your MainWindow and not of the QTextEdit.
    You have two options:
    1. Use an event filter - this way you don't need to subclass QTextEdit.
    Look in the docs for the QObejct::installEventFilter() they have a "key eater" example there.
    2. Subclass QTextEdit and re implement its keyPressEvent().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with keypressevent inside textedit

    Hi, high_flyer, thanks for the reply, im interested if possible in using option 2. (Subclass QTextEdit and re implement its keyPressEvent()). i understand the use of subclassing from my programming class, but applying to Qt is a different whole different lvl to me,
    i search the forum the nearest explanation of subclassing is

    http://www.qtcentre.org/threads/4090...t-on-a-QDialog


    the author said theres not much goes into detail on how exactly to subclass the object, (i tried but mostly come with error on ambiguity and no matching function for call to) is there any example on how to apply this , thanks again

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem with keypressevent inside textedit

    I am sorry, but sub classing is C++ basics, and its off topic for this forum.
    You will be more helped on C++ tutorial sites.
    However feel free to ask any Qt related questions you have.
    If you are not comfortable with sub classing, why do you go for option 2 and not option 1?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    serula (28th April 2011)

  6. #5
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with keypressevent inside textedit

    Thanks again for the advice ..

Similar Threads

  1. Inserting label inside textEdit widget
    By dshan in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2011, 12:17
  2. insert hyperlink(URL) inside the TextEdit..
    By rleojoseph in forum Qt Programming
    Replies: 10
    Last Post: 6th January 2011, 08:15
  3. Replies: 6
    Last Post: 13th May 2008, 14:19
  4. keyPressEvent problem
    By amulya in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2008, 14:16
  5. QLineEdit keyPressEvent problem
    By impeteperry in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2007, 17:57

Tags for this Thread

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.