So are you saying that the FilterObject is not the window? it is the ?what? the text box or is the text box a child widget of the filter - I'm cornfused.
So are you saying that the FilterObject is not the window? it is the ?what? the text box or is the text box a child widget of the filter - I'm cornfused.
No, it is the window, but you just don't do it correctly?bool QObject::eventFilter ( QObject * watched, QEvent * event ) [virtual]
Filters events if this object has been installed as an event filter for the watched object.
In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
Example:
class MainWindow : public QMainWindow
{
public:
MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *ev);
private:
QTextEdit *textEdit;
};
MainWindow::MainWindow()
{
textEdit = new QTextEdit;
setCentralWidget(textEdit);
textEdit->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == textEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
qDebug() << "Ate key press" << keyEvent->key();
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
}
Notice in the example above that unhandled events are passed to the base class's eventFilter() function, since the base class might have reimplemented eventFilter() for its own internal purposes.
Warning: If you delete the receiver object in this function, be sure to return true. Otherwise, Qt will forward the event to the deleted object and the program might crash.
See also installEventFilter().
From the code I conclude that you want to filter events for the Main Widnow. Is that so?
See the code bellow. It is an illustration on how event filters should be used.
There, the main window is the filter for one of its children, not for itself( that definitely can't work).
Regards
Ok, the missing piece you supplied was where to install the event filter. I changed my code and now the enter key fires the event filter. However, none of the widgets on the MainWindow show up. I even moved the installEventFilter() to the end of the constructor and still nothing shows on the window - as though the control with the focus was the only one on the form.
You probably don't let any other events pass either.
Could you post your new event filter?
Events that do not concern you should be ignored by the filter.
Regards
// in constructor
setCentralWidget(txtName);
txtName->installEventFilter(this);
//
////////// event filter /////////////
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == txtName && event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Enter)
{
// Special tab handling
qDebug("Enter Key Pressed...");
return true;
}
else if (keyEvent->key() == Qt::Key_Return)
{
// Special tab handling
qDebug("Enter Key Pressed...");
return true;
}
else
{
return QMainWindow::eventFilter(object, event);
}
}
else
{
return QMainWindow::eventFilter(object, event);
}
}
//
I will eventually add four other textedit controls to the statements for the same action.
The code seems OK.
You do this:
Qt Code:
setCentralWidget(txtName);To copy to clipboard, switch view to plain text mode
What other widgets do you refer to?
This makes the text edit take over the entire client area in the window.
To add other widgets:
Qt Code:
l->addWidget(some widget); l->addWidget(some widget); l->addWidget(some widget); ... [B]l->addWidget(txtEdit)[/B]; ... l->addWidget(some widget); w->setLayout(l); this->setCentralWidget(w);To copy to clipboard, switch view to plain text mode
This is just an example. You can use whatever layout you like.
The point is that in order to add multiple widgets in a main window client area, you need to have a container for them.
This is why you don't see any other widgets.
Regards
Ok I worked it out. I did not need to set the control as the central widget just installEventFilter(). When I changed the code it all worked. Thanks a million for your help.
You're welcome.
Bookmarks