我正在实现一个应用程序,其中一个侧面小部件可以扩展/缩小,因此另一个小部件必须缩小/扩展。 (或者可以在该窗口小部件上显示侧窗口小部件,无关紧要,我接受这两种实现)。它看起来像这样:
以下是我的代码的一部分:
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'))
如何在代码中进行此类调整?
答案 0 :(得分:2)
要获得该行为,您可以通过在侧面小部件的中间嵌入旋转按钮来使用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_())
输出: