Results 1 to 6 of 6

Thread: QPlainTextEdit shortcut

  1. #1
    Join Date
    Sep 2010
    Location
    Zamość, Poland
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60

    Default QPlainTextEdit shortcut

    Hi.
    I writing app (text editor), where I need to commit changes by clicking a return/enter key.
    To edit text I using QPlainTextEdit, but my shortcut not working.
    I don't need to have new lines in text.
    I tried to replace QPlainTextEdit with QLineEdit, but QLineEdit don't have line wrapping.
    How can I implement this shortcut or how can I get line wrapping in QLineEdit.

    Sorry for my bad english.

    Code for shortcut and QLineEdit:

    Qt Code:
    1. QShortcut* shortcut = new QShortcut(QKeySequence("Return"), ui->lineEdit);
    2. connect(shortcut, SIGNAL(activated()), this, SLOT(confirm()));
    To copy to clipboard, switch view to plain text mode 
    Last edited by Sölve; 10th May 2011 at 16:52. Reason: spelling corrections

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPlainTextEdit shortcut

    Try this, if it fits your need,

    Sub-class QPlainTextEdit, and reimplement QPlainTextEdit::keyPressEvent() and QPlainTextEdit::keyReleaseEvent() Protected Functions.

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

    Sölve (13th May 2011)

  4. #3
    Join Date
    Sep 2010
    Location
    Zamość, Poland
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QPlainTextEdit shortcut

    I created sub-class for QPlainTextEdit and reimplemented QPlainTextEdit::keyPressEvent(), but when I trying to write something, there's nothing appears.

    I tried to reimplement QPlainTextEdit::keyReleaseEvent(), but when I clicking "Return" new line appears and then changes are confirming.

    Code:

    Qt Code:
    1. void EPlainTextEdit::keyPressEvent(QKeyEvent *e)
    2. {
    3. if(e->key() == 0x01000004 or e->key() == 0x01000005)
    4. {
    5. eustia->confirm();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    "eustia" is object of main class.
    Last edited by Sölve; 10th May 2011 at 23:34. Reason: spelling corrections

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPlainTextEdit shortcut

    You may also need to call the base class implementation QPlainTextEdit::keyPressEvent(e); before you implmentation, but record the key before calling it, just to be sure.

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

    Sölve (13th May 2011)

  7. #5
    Join Date
    Sep 2010
    Location
    Zamość, Poland
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QPlainTextEdit shortcut

    I called QPlainTextEdit:keyPressEvent, and now all is done

    Code:
    Qt Code:
    1. void EPlainTextEdit::keyPressEvent(QKeyEvent *e)
    2. {
    3. if(e->key() == 0x01000004 or e->key() == 0x01000005)
    4. {
    5. eustia->confirm();
    6. return;
    7. }
    8. QPlainTextEdit::keyPressEvent(e);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for the help.

  8. #6
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPlainTextEdit shortcut

    Thanks for the post - solved my problem. I decided to walk the controls such that normal Windows enter key behaviour could be implemented. So my handler looks like this:

    Qt Code:
    1. void TextEdit::keyPressEvent(QKeyEvent * in_pEvent)
    2. {
    3. switch (in_pEvent->key())
    4. {
    5. case Qt::Key_Return:
    6. case Qt::Key_Enter:
    7. {
    8. // Find next button in tab order
    9. QPushButton * pButton = NULL;
    10. while (!pButton)
    11. {
    12. focusNextChild();
    13. pButton = dynamic_cast<QPushButton *>(parentWidget()->focusWidget());
    14. }
    15.  
    16. // Click the button
    17. pButton->click();
    18.  
    19. // Restore the focus
    20. setFocus();
    21. break;
    22. }
    23.  
    24. default:
    25. QTextEdit::keyPressEvent(in_pEvent);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPlainTextEdit
    By deeee in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2010, 02:05
  2. QplainTextEdit
    By deeee in forum Qt Programming
    Replies: 6
    Last Post: 27th May 2010, 01:14
  3. QPlainTextEdit
    By Carlsberg in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2009, 07:12
  4. Shortcut
    By Voldemort in forum Qt Programming
    Replies: 16
    Last Post: 2nd May 2007, 21:51
  5. Trying to set shortcut
    By mikro in forum Newbie
    Replies: 2
    Last Post: 30th November 2006, 10:55

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.