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

Toolbar spacer does not expand to the right corner



  • Hello everyone this is my first post,

    I have been trying to set some of my items to the right of a toolbar. By searching for a solution I understand that the general methodology would be to create a spacer and set its size policy to Expanding. However by doing so I am only able to create a small gap between my items(shown between Configuration and Style):

    small spacing.PNG

    If I try to change its minimum size I apparently obtain my expected result, but this has the format I want only in full-screen.

    wanted result.PNG

    If I try resizing the window, the items to the right of the toolbar disappear and therefore the spacer does not adjust to the correct size, keeping the items to the right hidden.

    hidden items.PNG

    This is how I set up my spacer:

    QWidget* spacer = new QWidget();
    spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    spacer->setMinimumWidth(1180);
    m_fileToolBar->addWidget(spacer);
    

    My main goal is to have all items showing up in the toolbar no matter the window size.
    Could someone please help me? Thank you all in advance. (I'm working with C++)



  • @TommyTLC
    Maybe you need it because you are using a toolbar, I don't know. but why are you using a QWidget* spacer? Should/can you use a layout on the toolbar and use QSpacerItem, or more generally just QBoxLayout::addSpacing()? Maybe @VRonin's post at https://forum.qt.io/topic/65574/qtoolbar-widgets-spacing/2 indicates the approach?



  • @JonB Thank you for your reply. I have tried applying @VRonin 's method, but the situation that is created is still the same: some space is added to the toolbar, but the items to the far right are hidden when the window is not in full-screen.

    How would the other process you mentioned work? I have created a QSpacerItem this way:

    QSpacerItem* spacerItem;
    spacerItem->addSpacing();

    But I am not able to use addSpacing() as it's not a member of QSpacerItem.



  • Can't replicate the problem using Qt 5.15 or 6.0
    See minimal example below:

    #include <QApplication>
    #include <QMainWindow>
    #include <QToolBar>
    #include <QLabel>
    #include <QSpinBox>
    #include <QLineEdit>
    #include <QComboBox>
    int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
        QMainWindow wid;
        QToolBar* toolBar = new QToolBar;
        toolBar->addWidget(new QLabel("3DViewver"));
        toolBar->addWidget(new QLineEdit);
        toolBar->addWidget(new QLabel("OpenGL"));
        toolBar->addWidget(new QSpinBox);
        toolBar->addWidget(new QLabel("Configuration"));
        QWidget* spacer = new QWidget();
        spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        toolBar->addWidget(spacer);
        toolBar->addWidget(new QLabel("Style"));
        toolBar->addWidget(new QComboBox);
        wid.addToolBar(toolBar);
        wid.show();
        return app.exec();
    }
    


  • Another method I have tried to fix my problem with is having 2 toolbars; one with the left-to-right items and another one with the right-to-lefts ones. The issue I'm having with this is being able to dock toolbar2 to the right of toolbar1 so that they can both be on the same line. I can dock toolbar2 to the Top, but that means having it right below toolbar1. The problem here is not being able to specify the docking area by comparing the two toolbars.



  • @VRonin Thanks for your reply. Unfortunately I am still having the same issue where generating a spacer widget only adds a little bit of space between the items in the toolbar.



    1. Did you try the minimal example above?
    2. What version of Qt are you using?
    3. Do you customise any of the widgets on the right of the spacer?
      • if you give QSizePolicy::Expanding or minimum sizes to them they will be respected


  • @VRonin I did try to include the spacer just like you do in your minimal example, but the problem is still there, therefore the issue might be somewhere else. I am working with Qt version 5.15.

    This is all I have to the right of the spacer:

    m_fileToolBar->addWidget(new QLabel(tr("   Style:")));
    QAction* stylesAction = m_fileToolBar->addWidget(createComboBoxStyle());
    stylesAction->setToolTip(tr("Styles switcher"));
    
    m_fileToolBar->addSeparator(); 
    m_fileToolBar->addAction(m_aboutAction);
    

    Where m_fileToolBar is my toolBar and 2 actions as well as a separator and a label are added (these can be seen in the picture included in the original post).



  • I suspect the problem is not with the spacer widget but with something else in your code.
    We need to narrow down the list of suspects.
    Please answer the point 1 above and, in addition:

    1. what is the body of createComboBoxStyle?
    2. how do you create m_aboutAction?

  • Lifetime Qt Champion

    Hi
    Just as a note.
    You could apply a style sheet

    spacer->setStyleSheet("QWidget{ background-color: rgb(178, 255, 34);}");

    so its clear how it's placed.



  • @VRonin ok so I have tried creating a new project with your minimal example and that works perfectly! The problem is now understanding what's causing the issue in my project.

    1. m_styleSwitcher = new QComboBox(this);
      m_styleSwitcher->addItem(m_defaultStyleAction->text(), Default);
      m_styleSwitcher->addItem(m_fusionStyleAction->text(), Fusion);
      m_styleSwitcher->addItem(QStringLiteral("VisualStudio2019 ") + m_themeBlueAction->text(), VS2019Blue);
      m_styleSwitcher->addItem(QStringLiteral("VisualStudio2019 ") + m_themeDarkAction->text(), VS2019Dark);
      m_styleSwitcher->addItem(QStringLiteral("VisualStudio2019 ") + m_themeLightAction->text(), VS2019Light);
      m_styleSwitcher->setCurrentIndex(static_cast<int>(m_appStyle));
      connect(m_styleSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(styleIndexChanged(int)));
      return m_styleSwitcher;

    2. m_aboutAction is created in the .h file:

      QAction* m_aboutAction;



  • @mrjj This might actually help understanding the cause of the issue. I have applied the style sheet to the spacer. When the window is in full screen mode I can correctly see the spacer object:

    visible spacer.PNG

    While having the application in a smaller window I do not see the style sheet:

    not visible spacer.PNG


  • Lifetime Qt Champion

    @TommyTLC
    Ok so does it go into the >> or is then completely gone ?



  • @mrjj in the >> I only see my m_aboutAction. It looks like the spacer disappears when the application is not in full screen.


  • Lifetime Qt Champion

    @TommyTLC
    That i have not seen before. :)
    Could you give your spacer object a name with setObjectName and
    then do
    ui->menubar->dumpObjectTree();
    (adjust to name)

    Then it will show all its sub objexts like

    QMenuBar::menubar 
        QToolButton::qt_menubar_ext_button 
        QMenu::menuMENU 
            QAction:: 
        QMenu::menuMENU2 
            QAction:: 
        QMenu::menuMENU3 
            QAction:: 
    

    just to see if its still in or really gone. Its kinda hard for a widget to just fall off as it's owned by the parent.



  • @mrjj This is what I have done:

    spacer->setObjectName("spacerName");
    ui->m_fileToolBar->dumpObjectTree();

    It looks like this function does not allow the use of "ui". If I follow the error's suggestion and use a pointer type toward m_fileToolBar I get another error saying that m_fileToolBar is not a member of the Ui::MainWindow class.


  • Lifetime Qt Champion

    Hi
    the name m_fileToolBar suggest to me its not in a UI
    so its just
    m_fileToolBar->dumpObjectTree();



  • @mrjj Thank you! Here's the output (I have given my spacer object the name "spacerName"):

    Qtitan::DockToolBar::
    Qtitan::DockToolBarLayout::
    Qtitan::DockToolBarExtension::
    QMenu::
    QAction::
    Qtitan::ToolButton::
    QWidgetAction::
    QLineEdit::
    QWidgetLineControl::
    Qtitan::ToolButton::
    QWidgetAction::
    QSpinBox::
    QLineEdit::qt_spinbox_lineedit
    QWidgetLineControl::
    QValidator::qt_spinboxvalidator
    Qtitan::ToolButton::
    QWidgetAction::
    QWidget::spacerName


  • Lifetime Qt Champion

    @TommyTLC
    Ok so it is still there :)
    Then i would
    qDebug() << "size" << qDebug() << spacer->geometry();
    and it will show something like
    QRect(747,339 75x23)

    as it could be fun to see its location and size.



  • @mrjj This is where my spacer is located/its size: QRect(0,0 1180x480)


  • Lifetime Qt Champion

    @TommyTLC
    so its at o,0 and 1180 wide o.O and almost 500 in height.
    but we clearly dont see that.
    can you try
    qDebug() << "vis" << spacer->isVisible();

    i was wondering if this happens due to
    spacer->setMinimumWidth(1180);
    as i have seen widgets do odd stuff when not room enough.



  • @mrjj vis false


  • Lifetime Qt Champion

    @TommyTLC
    ok so its get hidden for some odd reason.
    do you use the do anything to the spacer variable after it has been inserted ?
    also try
    spacer->show();
    after its gone. ( like via a button)
    and lets see what happens.



  • @mrjj This is all I do with the spacer:

    QWidget* spacer = new QWidget();
    spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    spacer->setMinimumWidth(1180);
    m_fileToolBar->addWidget(spacer);
    

    it gets created, I set the SizePolicy and the minimumWidth and then I assign it to the toolBar.


  • Lifetime Qt Champion

    @TommyTLC
    Ok so its not hidden by user code.
    Could you try a smaller minimum and see like
    spacer->setMinimumWidth(200);

    I know its not the goal but just to see if it still happens.



  • @TommyTLC said in Toolbar spacer does not expand to the right corner:

    in the >> I only see my m_aboutAction

    you do not see the style combo either?

    Did you overload resizeEvent?

    Does the "Configuration" label on the left have something special?



  • @mrjj said in Toolbar spacer does not expand to the right corner:

    Ok so its not hidden by user code.

    If it goes into >> then it will be hidden. I'm not sure this test is conclusive



  • @mrjj By changing the minimum size I see the spacer:

    visible spacer.PNG

    But the items to the right can still be hidden by resizing the window:

    not visible spacer.PNG



  • @VRonin Correct, the only thing I see by clicking the ">>" is the m_aboutAction.
    The configuration label is an action. When clicked it generates a dialog you can interact with.
    I am not sure what overlading the resizeEvent means.


  • Lifetime Qt Champion

    Hi

    • I am not sure what overlading the resizeEvent means.

    Like for MainWindow, you have that function implemented in your code.

    I have a feeling it the
    setMinimumWidth that is the culprit of this.



  • @mrjj Getting rid of the minimumWidth only leaves a small gap between the items in the toolBar. I don’t know what else I could try :(


  • Lifetime Qt Champion

    @TommyTLC
    Ok so it dont grow ?

    Hmm. What version of Qt are you using ?

    the code VRonin works perfectly in 5.15

    alt text

    But you get another result ?



  • @mrjj I’m working on the 5.15. His code DOES work perfectly, but for some reasons it has issues with my project. The spacer does not push the items all the way to the right. That’s why I was trying to set its minimumWidth.


  • Lifetime Qt Champion

    Hi,

    Then you should start by using @VRonin's code in place of yours. Once you confirm it's working, you can step by step replace the widgets with the ones you are using until it breaks again.


Log in to reply