【问题标题】:Mutual resizing of widgets小部件的相互调整大小
【发布时间】:2023-03-22 09:24:02
【问题描述】:

我正在实现一个应用程序,其中一个侧面小部件可以展开/缩小,因此另一个小部件必须缩小/展开。 (或者侧面小部件可以显示在该小部件上,没关系,我接受这两种实现)。它看起来像这样:

这是我的代码的一部分:

class AppView:
    def __init__(self):
        self._mainWindow = QDialog(None)
        self._schedule = ScheduleView(self._mainWindow)
        self._schedule.setMinimumWidth(25)
        self._schedule.setMaximumWidth(250)
        self._tutorial = TutorialView(self._mainWindow)
        self._schedule.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        self._tutorial.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        layout = QHBoxLayout()
        layout.addWidget(self._schedule)
        layout.addWidget(self._tutorial)
        layout.setSpacing(0)
        layout.setContentsMargins(0, 0, 0, 1)
        self._mainWindow.setLayout(layout)

class TutorialView(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent=parent)
        self._presenter = TutorialPresenter(self)
        self.reqReprSections.connect(self.setModel)
        self.reqReprTopics.connect(self.setModel)
        self._widget = QQuickWidget(self)
        self._widget.rootContext().setContextProperty('tutorialView', self)
        self._widget.setSource(QUrl('modules/manual/manualForm/TutorialForm.qml'))

class ScheduleView(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent=parent)
        self._presenter = SchedulePresenter(self)
        self._widget = QQuickWidget(self)
        self._widget.setResizeMode(QQuickWidget.SizeViewToRootObject)
        self._widget.rootContext().setContextProperty('scheduleView', self)
        self._widget.rootContext().setContextProperty('groupsModel', self)
        self._widget.setSource(QUrl('modules/schedule/scheduleForm/ScheduleForm.qml'))

如何在代码中进行这种调整大小?

【问题讨论】:

  • 你的问题是什么?
  • 您可以使用QSplitter,或类似this

标签: python python-2.7 pyqt qt5


【解决方案1】:

要获得这种行为,您可以通过在侧面小部件中间嵌入一个旋转按钮来使用 QHBoxLayout。您必须更改左侧小部件的大小策略,使其不会扩展。

要实现旋转按钮,除了修改大小策略使其垂直而不是水平扩展之外,您还必须重写 paintEvent 方法。

class ShrinkExpandButton(QPushButton):
    def __init__(self, *args, **kwargs):
        QPushButton.__init__(self, *args, **kwargs)
        self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
        self.setFixedWidth(2*self.fontMetrics().height())

    def paintEvent(self, event):
        painter = QStylePainter(self)
        painter.rotate(-90)
        painter.translate(-self.height(), 0)
        option = QStyleOptionButton()
        self.initStyleOption(option)
        size = option.rect.size()
        size.transpose()
        option.rect.setSize(size)
        painter.drawControl(QStyle.CE_PushButton, option)


class ShrinkExpandWidget(QWidget):
    def __init__(self, leftWidget, rightWiget, text, parent=None):
        QWidget.__init__(self, parent)
        button = ShrinkExpandButton(text, self)

        self.setLayout(QHBoxLayout())
        self.layout().setSpacing(0)
        self.layout().addWidget(leftWidget)
        self.layout().addWidget(button)
        self.layout().addWidget(rightWiget)
        leftWidget.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
        button.clicked.connect(lambda: leftWidget.setVisible(not leftWidget.isVisible()))

例子:

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    listWidget = QListWidget()
    for i in range(20):
        listWidget.addItem("{}".format(i))

    tableWidget = QTableWidget()
    tableWidget.setColumnCount(10)
    tableWidget.setRowCount(20)
    for i in range(tableWidget.rowCount()):
        for j in range(tableWidget.columnCount()):
            tableWidget.setItem(i, j, QTableWidgetItem("({}, {})".format(i, j)))
    listWidget.setFixedWidth(240)
    w = ShrinkExpandWidget(listWidget, tableWidget, "Shrink - Expand")
    w.resize(720, 480)
    w.show()
    sys.exit(app.exec_())

输出:

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2011-05-18
    相关资源
    最近更新 更多