PDA

View Full Version : keyPressEvent of QPlainTextEdit on a QDialog



TheVirus
22nd April 2011, 04:26
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


class MyDialog : public QDialog, public QPlainTextEdit
{
Q_OBJECT
...


MyDialog.cpp


MyDialog::MyDialog(QWidget *parent) :
QDialog(parent), QPlainTextEdit(parent),
ui(new Ui::MyDialog)
{
...


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


#include <QPlainTextEdit>

MyDialog::MyDialog ...

QPlainTextEdit::keyPressEvent(QKeyEvent *event) {
...
}


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


QPlainTextEdit::keyPressEvent(QKeyEvent *event) {
...
QWidget::keyPressEvent(event);
//or
event->ignore();
}


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



MyDialog::keyPressEvent(QKeyEvent *event) {
cout << "dialog event" << endl;
event->ignore();
//or
QDialog::keyPressEvent(event);
}


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.

stampede
22nd April 2011, 07:22
You want to have custom plain text edit, so subclass QPlainTextEdit:

class MyPlainTextEdit : public QPlainTextEdit{ ...
...
protected:
virtual void keyPressEvent( QKeyEvent * );

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.

TheVirus
22nd April 2011, 17:45
You want to have custom plain text edit, so subclass QPlainTextEdit:

class MyPlainTextEdit : public QPlainTextEdit{ ...
...
protected:
virtual void keyPressEvent( QKeyEvent * );

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.