[Solved] How to propagate keyPressEvent on different qt QMainWindow
-
I try some other class to control this strange case.
Yes, when I tried with QListWidget and also QPushButton, it was able to propogate to the MainWindow parent widget.
you refer to use this?
yes, that works for you right?Yes, but also QPushButton have their keyPressEvent reimplement from QWidget!
I stand corrected.Maybe there is a bug in some class that not propagate the event to the parent widget
I'm not sure if this is a bug. The window flags for a Dialog and Popup make it a top-level window. I think the intended behavior is so. So you have to use the workaround you mentioned earlier.
But, if you override the window flag of the dialog and set it to Qt::Widget, the events get propagated, but the UI behaviour of course changes. :) -
bq. es, that works for you right?
Yes, but with that workaround i call directly the function of the parent and i get to set public in a parent.
bq. The window flags for a Dialog and Popup make it a top-level window. I think the intended behavior is so. So you have to use the workaround you mentioned earlier.
But, if you override the window flag of the dialog and set it to Qt::Widget, the events get propagated, but the UI behaviour of course changes.Yes, but setting Qwidget flag deform my window form ( i try in the example posted and the result is one window that incorporate the two form!) and setting other such a QWindow or QDialog block the event.
Maybe, there are some Qt Troll that can post their impression about this, but i think they are at Qt Developer Days :)
-
yes now even i'm curious to know what they'd say :)
-
Well, after few days i think that QMainWindow is a top windows,for me QMainWindow is a event end, and maybe this is the problem because the event is not propagate!
Maybe there are some class such as QMainWindow,QDialog ecc.. that prevent the propagation of the event.If i want propagate the event i have to create an eventFilter on a parentWidget, and install it on a children. See "doc":http://doc.qt.nokia.com/4.7/eventsandfilters.html#event-filters
Now the code of the example is this:
//In the parent
@bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == form && event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug() << "event from children";
MainWindow::keyPressEvent(keyEvent); //Use the event in parent using its keyPressEvent()
return QObject::eventFilter(object, event);
}return QObject::eventFilter(object, event);
}
@//Children
@test::test(QWidget *parent) : QMainWindow(parent), ui(new Ui::test)
{
ui->setupUi(this);this->setWindowFlags(Qt::Dialog); this->installEventFilter(parent); //Installing the filter
}@
This is not very easy but clean, and work :)
But now qt developer days are end and i want a comment of a qt troll about this long post :)
-
yes this is definitely cleaner :) thanks for the post
-
Hello all,
Indeed the events are propagated to the parent but it stops to the top level parent (ie. the window). This is to make sure that all events get sensibly propagated tovisual parents.
So if you want to propagate a key event from on main window to another one, you'll have to do it manually. -
thanks for the clarification Thierry :)
-
bq. Hello all,
Indeed the events are propagated to the parent but it stops to the top level parent (ie. the window). This is to make sure that all events get sensibly propagated tovisual parents.
So if you want to propagate a key event from on main window to another one, you’ll have to do it manually.Thank's thierry, this tips is in the documentation of qt?