Results 1 to 3 of 3

Thread: keyPressEvent of QPlainTextEdit on a QDialog

  1. #1
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default keyPressEvent of QPlainTextEdit on a QDialog

    I'm trying to capture key events in my QPlainTextEdit that's inside of a QDialog widget.

    Setup:

    MainWindow has a push button. Pressing this button opens the dialog (MyDialog) which has the QPlainTextEdit (textEdit) inside it. This was designed in the Designer and I reference the .ui file rather than building it from code.

    Details:

    I've seen people say 'subclass the widget and reimplement the keyPressEvent'. The problem is, no one goes into detail of how, exactly, to subclass the QPlainTextEdit object. I've tried the following:

    MyDialog.h
    Qt Code:
    1. class MyDialog : public QDialog, public QPlainTextEdit
    2. {
    3. Q_OBJECT
    4. ...
    To copy to clipboard, switch view to plain text mode 

    MyDialog.cpp
    Qt Code:
    1. MyDialog::MyDialog(QWidget *parent) :
    2. QDialog(parent), QPlainTextEdit(parent),
    3. ui(new Ui::MyDialog)
    4. {
    5. ...
    To copy to clipboard, switch view to plain text mode 

    But this gives me errors on essentially everything.
    'ambiguous access of connect'
    'connect identifier not found'
    'show identifier not found'
    etc.

    I've also tried the following:

    MyDialog.cpp
    Qt Code:
    1. #include <QPlainTextEdit>
    2.  
    3. MyDialog::MyDialog ...
    4.  
    5. QPlainTextEdit::keyPressEvent(QKeyEvent *event) {
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    But this traps all keys and doesn't send them to the parent control and I'm also guessing this is completely wrong. I've tried adding:

    MyDialog.cpp
    Qt Code:
    1. QPlainTextEdit::keyPressEvent(QKeyEvent *event) {
    2. ...
    3. QWidget::keyPressEvent(event);
    4. //or
    5. event->ignore();
    6. }
    To copy to clipboard, switch view to plain text mode 

    But that doesn't work either. I've tried reimplementing the keyPressEvent for MyDialog:

    Qt Code:
    1. MyDialog::keyPressEvent(QKeyEvent *event) {
    2. cout << "dialog event" << endl;
    3. event->ignore();
    4. //or
    5. QDialog::keyPressEvent(event);
    6. }
    To copy to clipboard, switch view to plain text mode 

    But nothing gets passed on. I see the 'dialog event' in the console, so it is activating, but no event is passed on and the keyboard input is never displayed. I'm all out of ideas and no matter what I search for I cannot find an example or explanation of what I'm trying to do.

    Thanks.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: keyPressEvent of QPlainTextEdit on a QDialog

    You want to have custom plain text edit, so subclass QPlainTextEdit:
    Qt Code:
    1. class MyPlainTextEdit : public QPlainTextEdit{ ...
    2. ...
    3. protected:
    4. virtual void keyPressEvent( QKeyEvent * );
    To copy to clipboard, switch view to plain text mode 
    and use "Promote To" feature of the Designer - place QPlainTextEdit on the .ui form, right click and select "Promote To", type MyPlainTextEdit as class name (or whatever name you'll pick). There are plenty of threads on this subject in the forum, search for "custom widgets" or something like that.

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

    TheVirus (22nd April 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: keyPressEvent of QPlainTextEdit on a QDialog

    Quote Originally Posted by stampede View Post
    You want to have custom plain text edit, so subclass QPlainTextEdit:
    Qt Code:
    1. class MyPlainTextEdit : public QPlainTextEdit{ ...
    2. ...
    3. protected:
    4. virtual void keyPressEvent( QKeyEvent * );
    To copy to clipboard, switch view to plain text mode 
    and use "Promote To" feature of the Designer - place QPlainTextEdit on the .ui form, right click and select "Promote To", type MyPlainTextEdit as class name (or whatever name you'll pick). There are plenty of threads on this subject in the forum, search for "custom widgets" or something like that.
    Ah, I would have never have thought of something like that. I'll give it a shot, thanks.

Similar Threads

  1. Replies: 9
    Last Post: 25th March 2011, 21:22
  2. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 11:35
  3. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 02:12
  4. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02
  5. Getting real key in keyPressEvent
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2008, 14:39

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.