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

Add QWidget above QToolBar in QMainWindow



  • Hello,

    I have a QMainWindow with a central widget, left docked widget and a right docked widget.
    I also have a toolbar at the top.

    Below screenshot depicts my current layout structure.
    438f5a48-4e2b-4884-97fe-9a253ea45410-image.png

    I would like to add a widget on top of the toolbar that would contain the user info like name, DOB etc.
    Expected layout is as depicted by the below image
    13180d42-c84a-461f-9dd7-df973030b05e-image.png

    Is there a way to do it??
    I tried creating a vertical layout with title widget and toolbar, and added that layout above central widget.
    But it doesn't help.

    Note: Title widget here is not the "window title", i want to have a separate widget on top of toolbar.

    Can anyone give me a heads up on this ? or is this atleast doable?


  • Moderators

    @Rizwan94 said:

    I tried creating a vertical layout with title widget and toolbar, and added that layout above central widget.

    That's close, but you need one extra step.
    Create a plain QWidget, set a vertical layout on it, add your widget and toolbar to it and then use setMenuWidget() on your main window.

    EDIT: Or you can even make it simpler - just use your title widget directly as the menu bar.



  • @Rizwan94
    Before someone else comes up with some clever way to achieve it....

    A QMainWindow has its layout as per https://doc.qt.io/qt-5/qmainwindow.html#details picture, and I don't see it offering you to change that framework. So using a QMainWindow feels like a square peg & a round hole. Are you tied to QMainWindow. If it's your own code and not tied, I'd have a QWidget with the furniture bits added to it. Like I say, until someone says yours can be done with a one-liner on QMainWindow.

    Another possibility is to try inherited QWidget::setLayout() on a QMainWindow. Find out how they've laid it out, and insert your extra title widget before the toolbar ( how did you do your "added that layout above central widget"?). But don't blame me if it doesn't work or makes something go wrong!


  • Moderators

    @Rizwan94 said:

    I tried creating a vertical layout with title widget and toolbar, and added that layout above central widget.

    That's close, but you need one extra step.
    Create a plain QWidget, set a vertical layout on it, add your widget and toolbar to it and then use setMenuWidget() on your main window.

    EDIT: Or you can even make it simpler - just use your title widget directly as the menu bar.



  • Thanks @Chris-Kawa
    Works like a charm!!