Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I place a qlabel over qwidget placed in a window container?



  • Hello all,

    I have a widget in which I am displaying some graph data (3D scatter plot). I display as it so:

    QHBoxLayout* const layout = new QHBoxLayout(ui->graph_display_widget);
    layout ->setMargin(0);
    layout ->addWidget(QWidget::createWindowContainer(&graph, this));
    

    This is done in the main window constructor. 'graph_display_widget' is created in the UI editor inside of a frame.

    It displays well, however I would like to place a legend over the display widget. So in the UI editor, I created a qlabel 'legend' to the dimensions I want and drag it over the graph_display_widget, and in code I do:

    QPixmap legend_pix (":/images/legend.png");
    ui->legend->setPixmap(legend_pix);
    

    The image loads correctly when not placed over the graph_display_widget in the UI editor, but when I try and place it over the graph_display_widget, the graph_display_widget comes out on top instead. Not matter what I do, graph_display_widget is always on top.

    I've tried nesting the qlabel in another frame, widget, and the other containers and placing them over graph_display_widget, but graph_display_widget will always display on top. I've tried moving graph_display_widget to the bottom and the other widgets to the top using the context menu, I even tried:

    ui->graph_display_widget->lower();
    ui->legend->raise();
    

    Same result.

    Any help would be greatly appreciated!


  • Moderators

    @new-qt_user-2022 Unfortunately, this is a known limitation (see https://doc.qt.io/qt-5/qwidget.html#createWindowContainer ): "The embedded window will stack on top of the widget hierarchy as an opaque box. The stacking order of multiple overlapping window container instances is undefined."

    So, it is currently not possible to render a widget on top of an embedded window.

    EDIT: From your other posts, it sounds like you are using the Qt Data Visualization module? Perhaps you can implement the scatter graph and legend in QML, then put it in a QQuickWidget. See https://doc.qt.io/qt-5/qtdatavisualization-qmllegend-example.html -- Just replace the ColumnLayout + LegendItems with an Image


  • Moderators

    @new-qt_user-2022 Unfortunately, this is a known limitation (see https://doc.qt.io/qt-5/qwidget.html#createWindowContainer ): "The embedded window will stack on top of the widget hierarchy as an opaque box. The stacking order of multiple overlapping window container instances is undefined."

    So, it is currently not possible to render a widget on top of an embedded window.

    EDIT: From your other posts, it sounds like you are using the Qt Data Visualization module? Perhaps you can implement the scatter graph and legend in QML, then put it in a QQuickWidget. See https://doc.qt.io/qt-5/qtdatavisualization-qmllegend-example.html -- Just replace the ColumnLayout + LegendItems with an Image



  • @JKSH I see, thank you for that. I will check out the example you provided.

    I have never used QML and have done things using purely with C++. Yes I was using the data visualization module and had everything pretty much done (display of two custom meshes, custom mesh for data points, etc).

    How difficult would it be to convert the code? Or instead of a window container, is there a way to display the visualization graph in a graphics view widget or other widget where it is possible to stack a label over it as I described?

    Thank you again!


  • Moderators

    @new-qt_user-2022 said in How can I place a qlabel over qwidget placed in a window container?:

    How difficult would it be to convert the code?

    I'm not sure, sorry; it partly depends on how complex your C++ code is.

    Or instead of a window container, is there a way to display the visualization graph in a graphics view widget or other widget where it is possible to stack a label over it as I described?

    The C++ API for Qt Data Visualization is based on QWindow. No matter what widget you use, you'd need QWidget::createWindowContainer() to embed a QWindow in any widget.



  • @JKSH Indeed, I found this in the documentation:

    "The call to QWidget::createWindowContainer is required, as all data visualization graph classes (Q3DBars, Q3DScatter, and Q3DSurface) inherit QWindow. Any class inheriting QWindow cannot be used as a widget any other way."

    --Scatter Example

    ...bummer.

    As for converting C++ to QML, according to the documentation, it should be straight forward. There is almost an equivalent QML object for every C++ object. I'm hoping I can just copy my Q3DScatter graph object to the QML equivalent and hit display or something.

    Gonna spend the week watching some QML YouTube videos in the meantime.

    Thanks again!


  • Moderators

    @new-qt_user-2022 All the best! Feel free to post new questions if you'd like further help.