qtqt5qtguiqmainwindowqtoolbar

toolbarBreak disappears when deleting QToolBar


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);
}

Solution

  • 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.