Important: Please read the Qt Code of Conduct -

How to center a QToolBar at QMainWindow?

  • Hi,
    I have been looking for a solution for this question in several forums and I have found several approaches but I didn't like any of them.
    Using this line I can set my toolbar in the bottom of my app:

    addToolBar(Qt::BottomToolBarArea, myToolbar); 

    But it is always left-aligned, is there a way to set it in the middle of that bottom panel? Also, Is there a way to make it look similar to the dock of the OSX desktop?


  • Moderators

    To keep it simple you can insert two expanding dummies at the start and end of the toolbar:

    auto dummy1 = new QWidget(this);
    dummy1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    auto dummy2 = new QWidget(this);
    dummy2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    auto tb = new QToolBar();
    addToolBar(Qt::BottomToolBarArea, tb);

  • Thanks! It worked perfect for me :)

    PS: I still miss the day you could do something like:

    addToolBar(Qt::BottomToolBarArea|Qt::CenterToolBarArea , myToolbar); 

  • Moderators

    Toolbar area in main window and buttons alignment within a toolbar are two entirely separate concepts so it wouldn't make sense to put these two options together.
    Would be nice however if there was something like QToolBar::setAlignment(Qt::Alignment), although toolbars can be placed on the sides too, so directions become a little fuzzy.

Log in to reply