Qt6 topics merged with General

QToolBar widgets spacing

  • I am adding a QSpinBox and a QComboBox to a QToolBar using QToolBar::addWidget(). It actually does what it is supposed to and I can successfully use the widgets. However, I am not happy with the spacing of the widgets. The two widgets are glued together one next to each other, there's not a single pixel of space in between them:


    How can I change this? I tried to play around with QToolBar::setContentMargins(10, 10, 10, 10) but that only added padding to the bottom of the widgets.
    Basically I would like them to be spaced apart like when I add a separator between them using QToolBar::addSeparator():

    Expected result

    My code looks like this:

    QToolBar* toolbarDocument = new QToolBar;
    toolbarDocument->addWidget(new QSpinBox);
    toolbarDocument->addWidget(new QComboBox);

  • Did you try doing it outside of the QToolBar?

    QWidget* spinWidget = new QWidget;
    QHBoxLayout* spinLay = new QHBoxLayout(spinWidget);
    spinLay.addWidget(new QSpinBox(spinWidget));
    spinLay.setContentsMargins(0,0,10,0); // Here you set the spacing
    QToolBar* toolbarDocument = new QToolBar;
    toolbarDocument->addWidget(spinWidget );
    toolbarDocument->addWidget(new QComboBox);

Log in to reply