Search code examples
c++qtqt4qwidget

Simple way to get the current docked area of a QToolbar


I'm working on a Qt project, where I need to get the toolbar positions at run-time. Is there any way to get the current positioning of a QToolbar inside a QMainWindow ?

Thanks.

EDIT:

Thanks for the answer and I got 75% working. But there is a problem in QMainWindow::toolBarArea(QToolBar * toolbar).

When I dock two toolbars in a single area (i.e. Bottom area), as below.

-----------------------------------
| Toolbar 1                       |
-----------------------------------
| Toolbar 2                       |
-----------------------------------

Then I save the area obtained from QMainWindow::toolBarArea, using QSettings and then load them back, it loads as below.

-----------------------------------
| Toolbar 1      | Toolbar 2      |
-----------------------------------

Is there any way to stop that as well?


Solution

  • Within the scope of QMainWindow, you can call QMainWindow::toolBarArea(QToolBar *toolbar) to get the toolbar position.

    Returns the Qt::ToolBarArea for toolbar. If toolbar has not been added to the main window, this function returns Qt::NoToolBarArea.

    It returns the enum:

    enum ToolBarArea {
            LeftToolBarArea = 0x1,
            RightToolBarArea = 0x2,
            TopToolBarArea = 0x4,
            BottomToolBarArea = 0x8,
    
            ToolBarArea_Mask = 0xf,
            AllToolBarAreas = ToolBarArea_Mask,
            NoToolBarArea = 0
        };