1

lets consider the following screenshot:

enter image description here

You can see that the top toolbar displays 2 rows; however to do so , in need to click on the >> at the top right (circled in red) and keep hovering hover the toolbar area, which can get a bit annoying.

Is there a way to keep the 2 rows of the toolbar always displaying?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jim jarnac
  • 4,804
  • 11
  • 51
  • 88

1 Answers1

2

The solution is:

  • Expand the QToolBar using the layout that in the implementation of the private API has a slot called setExpanded() that allows to expand the QToolBar.
  • Hide the button, and for this case it only worked to set the size to QSize(0, 0).
  • Deactivate the event Leave of the QToolBar so that it does not collapse.
from PyQt5 import QtCore, QtGui, QtWidgets


class ToolBar(QtWidgets.QToolBar):
    def __init__(self, parent=None):
        super().__init__(parent)
        lay = self.findChild(QtWidgets.QLayout)
        if lay is not None:
            lay.setExpanded(True)
        QtCore.QTimer.singleShot(0, self.on_timeout)

    @QtCore.pyqtSlot()
    def on_timeout(self):
        button = self.findChild(QtWidgets.QToolButton, "qt_toolbar_ext_button")
        if button is not None:
            button.setFixedSize(0, 0)

    def event(self, e):
        if e.type() == QtCore.QEvent.Leave:
            return True
        return super().event(e)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QMainWindow()
    toolbar = ToolBar()
    for i in range(20):
        toolbar.addAction("action{}".format(i))
    w.addToolBar(QtCore.Qt.TopToolBarArea, toolbar)

    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thx a lot your sample code works! However I wanted to use a smaller snippet of it by simply using `toolbar = self.addToolBar('toolbar') lay = self.findChild(QtWidgets.QLayout) lay.setExpanded(True)` in my code (where the toolbar is), but i receive an error `'QLayout' object has no attribute 'setExpanded'`, do you know why? – jim jarnac May 01 '19 at 02:24
  • 1
    @jimbasquiat in your case use: `lay = toolbar.findChild(QtWidgets.QLayout) ` – eyllanesc May 01 '19 at 02:25