How to allow both Qt C++ widget and QML widget in a QStackedLayout to both receive MousePressEvents?

  • I’m working on developing a small game. I’ve completed the first version of it, entirely using Qt C++. It includes a MainWindow::CentralWidget where the gameplay and mouse events occur, and a QDockWidget on the right side for a control panel of QSliders, etc. Development has been very easy because Qt’s framework provides all of the basic elements. However, we want to make the right-side control panel slide in and out with a mouseover, and using QML animation and easing curves moving between states seems to be the best/easiest/cleanest way to accomplish that. I discovered QStackedLayout and it seems to be a good way to overlay QML in one widget (for the control panel) overlapping the game area in another widget (using setStackingMode(QStackedLayout::StackAll), Qt4.7+). I got it integrated, but the problem is that I can’t figure out how to receive mousePressEvents in both of the stacked widgets.

    @ qmlViewerPtr = new QmlApplicationViewer;

    QWidget* centralWidgetPtr = new QWidget;
    centralGameLayoutPtr = new QStackedLayout;
    centralTopLayoutPtr->setCurrentIndex(1); // select the 2nd Widget to be on top.@

    It appears that depending on what order I add the widgets and which one is active determines which one receives the mouse events. However, I’d like for both of them to receive the mouse events. I guess the only alternative is for one widget to receive the event and pass the event on to the 2nd widget.

  • Two further related questions. After the game, I'm generating a second High Score Screen with names and scores, currently in a 2nd QStackedWidget. All of my score/name data is in the Qt side, in a QList<myHighScoreStruct>. I saw in the "QML/C++ integration slides": that you can use slots and Q_INVOKABLE to pass simple parameters, but only pass-by-value, so nothing complicated like passing a data structure.

    Possible solutions:

    1. Use QML animations to change to a HighScoreTable-like state (with a blank table), then at the end of the animation inform Qt's MainWindow, which switches to the HighScore part of the StackedWidget, making only it visible. So the Qt part draws the table. BUT how to know when the QML animation is finished?
    2. In Qt draw all of the text in a custom QWidget with transparent background, then expose that QWidget to QML so QML draws the custom QWidget (with high score data embedded) as well as any border graphics.

    I guess I'll try#2, but would appreciate any feedback on any of the other questions.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.