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
...
class MyDialog : public QDialog, public QPlainTextEdit
{
Q_OBJECT
...
To copy to clipboard, switch view to plain text mode
MyDialog.cpp
MyDialog
::MyDialog(QWidget *parent
) : QDialog(parent
), QPlainTextEdit
(parent
),
ui(new Ui::MyDialog)
{
...
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent), QPlainTextEdit(parent),
ui(new Ui::MyDialog)
{
...
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
#include <QPlainTextEdit>
MyDialog::MyDialog ...
QPlainTextEdit::keyPressEvent(QKeyEvent *event
) { ...
}
#include <QPlainTextEdit>
MyDialog::MyDialog ...
QPlainTextEdit::keyPressEvent(QKeyEvent *event) {
...
}
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
QPlainTextEdit
::keyPressEvent(QKeyEvent *event
) {...
//or
event->ignore();
}
QPlainTextEdit::keyPressEvent(QKeyEvent *event) {
...
QWidget::keyPressEvent(event);
//or
event->ignore();
}
To copy to clipboard, switch view to plain text mode
But that doesn't work either. I've tried reimplementing the keyPressEvent for MyDialog:
cout << "dialog event" << endl;
event->ignore();
//or
}
MyDialog::keyPressEvent(QKeyEvent *event) {
cout << "dialog event" << endl;
event->ignore();
//or
QDialog::keyPressEvent(event);
}
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.
Bookmarks