0

When I execute the following example code, I first create two toolbars with a break between it and then I delete the second toolbar after that. The problem is that the toolbarBreak also disappears when I do that.

When I reinitialize t2, there is no break anymore. Is the toolbarBreak really "gone" here? To fix this, I need to call addToolBarBreak(); before i reinitialize the toolbar. But do I create a duplicate of the toolbarBreak I had before?

Or should I call removeToolbarBreak(t2) before deleting t2?

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    auto* t1 = new QToolBar(this);
    t1->setMovable(false);
    t1->addAction("Hello t1 Action");
    auto* t2 = new QToolBar(this);
    t2->setMovable(false);
    t2->addAction("Hello t2 Action");

    addToolBar(t1);
    addToolBarBreak();
    addToolBar(t2);

    delete t2;
    t2 = new QToolBar(this);
    t2->setMovable(false);
    t2->addAction("Hello t2 Action");

    // addToolBarBreak(); // fixes the problem
    addToolBar(t2);
}
easysaesch
  • 159
  • 1
  • 14
  • 1
    Separators are added to toolbars; if you delete the toolbar from which you added the separator, yes, it will delete the separator as well. Rather than deleting your t2 object, try setting its visible property to false. – mzimmers Dec 07 '21 at 16:23
  • 1
    @mzimmers Note that the OP is adding a 'break' between toolbars in a `QMainWindow` not adding a separator between actions in a `QToolBar` -- they're not the same thing. – G.M. Dec 07 '21 at 16:48
  • 1
    Oops...my bad. I looked at the documentation for addTooBarBreak -- it's brief, but reading between the lines, it appears that the answer to the OP's question is "yes" it's really gone. I guess Qt figures that no break is called for when there's nothing after it. I wonder whether using insertToolBarBreak() might yield different results, though I doubt it. Thanks for catching my goof. – mzimmers Dec 07 '21 at 16:54
  • @mzimmers But you are right, the breaks seems to follow the same as separators (also in menus probably). – easysaesch Dec 08 '21 at 18:24

1 Answers1

0

Yes, the Layout takes care of it! I debugged the Qt code myself today. See here, in QToolBarAreaLayout::takeAt(int *x, int index) the break will be removed.

easysaesch
  • 159
  • 1
  • 14