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?

4

1 回答 1

2

解决方案是:

  • 使用在私有 API 的实现中具有一个名为 setExpanded() 的插槽的布局展开 QToolBar,该插槽允许展开 QToolBar。
  • 隐藏按钮,在这种情况下,它只能将大小设置为 QSize(0, 0)。
  • 停用 QToolBar 的 Leave 事件,使其不会塌陷。
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_())
于 2019-05-01T01:45:53.767 回答