QScrollArea with - multiple QWidgets w/ QTreeViews in each
-
I have a scroll area in dockwidget. each scroll area has 2 qwidgets (aka boxes)
DockWidgets->scrollArea->box1 + box2
Each box has this hierarchy:
(box)QWidget->QScrollArea->QLineEdit+ QTreeViewI have QTreeView bound to a datamodel that works well.
The following are issues:- both boxes are not covering the entire super parent dockwidget
- the qtreeview is always fixed to the init time size
- the qtreeview does not change its size when bounded to a model
when I select box1 its nice to have it fill in all the available vertical space and may be hide box2's content.
please help.MW.scrollArea = QtWidgets.QScrollArea() MW.scrollArea.setMinimumWidth(350) MW.scrollArea.setWidgetResizable(True) MW.vboxLeft = QtWidgets.QVBoxLayout(MW.scrollArea) MW.vboxLeft.setSpacing(1) MW.vboxLeft.setContentsMargins(0, 0, 0, 0) MW.v1 = CollapsibleWidget("Tree1", MW.scrollArea) MW.vboxLeft.addWidget(MW.v1) MW.v2 = CollapsibleWidget("Tree2", MW.scrollArea) MW.vboxLeft.addWidget(MW.v2) MW.vboxLeft.addStretch() MW.dockWidget = QtWidgets.QDockWidget("Layout") MW.dockWidget.setWidget(MW.scrollArea) MW.dockWidget.setFeatures( QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable ) MW.addDockWidget(Qt.LeftDockWidgetArea, MW.dockWidget)
CollapsibleWidget is inspired from a web link class CollapsibleWidget(QtWidgets.QWidget): def __init__(self, title="", parent=None): super().__init__(parent) self.toggleButton = QtWidgets.QToolButton(self) self.toggleButton.setText(title) self.toggleButton.setCheckable(True) self.toggleButton.setChecked(True) self.toggleButton.setStyleSheet("QToolButton { border: none; }") self.toggleButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) self.toggleButton.setArrowType(Qt.RightArrow) self.toggleButton.toggled.connect(self.onToggle) self.contentArea = QtWidgets.QScrollArea(self) self.contentArea.setMinimumHeight(0) self.contentArea.setMaximumHeight(0) self.contentArea.setWidgetResizable(True) self.contentArea.setFrameShape(QtWidgets.QFrame.NoFrame) self.contentArea.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) self.contentArea.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) # 0. search box self.searchLine = QtWidgets.QLineEdit(self.contentArea) self.searchLine.setPlaceholderText("Filter") self.treeView = QtWidgets.QTreeView(self.contentArea) self.treeView.setHeaderHidden(False) self.treeView.setRootIsDecorated(True) self.treeView.setContentsMargins(0, 0, 0, 0) self.treeView.setFont(ProjectSettings.commonFont) self.treeView.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) self.contentAreaLayout = QtWidgets.QVBoxLayout(self.contentArea) self.contentAreaLayout.setSpacing(0) self.contentAreaLayout.setContentsMargins(0, 0, 0, 0) self.contentAreaLayout.addWidget(self.searchLine) self.contentAreaLayout.addWidget(self.treeView) lay = QtWidgets.QVBoxLayout(self) lay.setSpacing(0) lay.setContentsMargins(0, 0, 0, 0) lay.addWidget(self.toggleButton) lay.addWidget(self.contentArea) self.animationGroup = QParallelAnimationGroup(self) contentHeight = self.contentAreaLayout.sizeHint().height() collapsedHeight = self.sizeHint().height() - self.contentArea.maximumHeight() self.setAnimation( QPropertyAnimation(self, b"minimumHeight"), collapsedHeight, contentHeight ) self.setAnimation( QPropertyAnimation(self, b"maximumHeight"), collapsedHeight, contentHeight ) self.setAnimation( QPropertyAnimation(self.contentArea, b"maximumHeight"), 0, contentHeight ) def setAnimation(self, animation, collapsedHeight, contentHeight): animation.setDuration(500) animation.setStartValue(collapsedHeight) animation.setEndValue(collapsedHeight + contentHeight) self.animationGroup.addAnimation(animation) def onToggle(self): arrowType = Qt.RightArrow direction = QAbstractAnimation.Backward if not self.toggleButton.isChecked(): arrowType = Qt.DownArrow direction = QAbstractAnimation.Forward self.toggleButton.setArrowType(arrowType) self.animationGroup.setDirection(direction) self.animationGroup.start()
-
Hi and welcome to devnet,
Do no set the layout on your QScrollArea.
Set it on a widget that will contain the rest and then set that widget on the QScrollArea.
-
Tried, but has the same affect. QTreeView does not take the entire space.
class CollapsibleWidget(QtWidgets.QWidget): def __init__(self, title="", parent=None): super().__init__(parent) self.toggleButton = QtWidgets.QToolButton(self) self.toggleButton.setText(title) self.toggleButton.setCheckable(True) self.toggleButton.setChecked(True) self.toggleButton.setStyleSheet("QToolButton { border: none; }") self.toggleButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) self.toggleButton.setArrowType(Qt.RightArrow) self.toggleButton.toggled.connect(self.onToggle) self.contentArea = QtWidgets.QWidget() # 0. search box self.searchLine = QtWidgets.QLineEdit(self.contentArea) self.searchLine.setPlaceholderText("Filter (n: Name; a: Address)") # 1.tree view self.treeView = QtWidgets.QTreeView(self.contentArea) self.treeView.setHeaderHidden(False) self.treeView.setRootIsDecorated(True) self.treeView.setContentsMargins(0, 0, 0, 0) self.treeView.setFont(ProjectSettings.commonFont) self.treeView.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) self.contentAreaLayout = QtWidgets.QVBoxLayout(self.contentArea) self.contentAreaLayout.setSpacing(0) self.contentAreaLayout.setContentsMargins(0, 0, 0, 0) self.contentAreaLayout.addWidget(self.searchLine) self.contentAreaLayout.addWidget(self.treeView) self.scrollArea = QtWidgets.QScrollArea() self.scrollArea.setMinimumHeight(0) self.scrollArea.setMaximumHeight(0) self.scrollArea.setWidgetResizable(True) self.scrollArea.setFrameShape(QtWidgets.QFrame.NoFrame) self.scrollArea.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) self.scrollArea.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) self.scrollArea.setWidget(self.contentArea) lay = QtWidgets.QVBoxLayout(self) lay.setSpacing(0) lay.setContentsMargins(0, 0, 0, 0) lay.addWidget(self.toggleButton) lay.addWidget(self.scrollArea) self.animationGroup = QParallelAnimationGroup(self) contentHeight = self.contentAreaLayout.sizeHint().height() collapsedHeight = self.sizeHint().height() - self.scrollArea.maximumHeight() self.setAnimation( QPropertyAnimation(self, b"minimumHeight"), collapsedHeight, contentHeight ) self.setAnimation( QPropertyAnimation(self, b"maximumHeight"), collapsedHeight, contentHeight ) self.setAnimation( QPropertyAnimation(self.scrollArea, b"maximumHeight"), 0, contentHeight ) print(self.scrollArea.maximumHeight(), self.maximumHeight()) def setAnimation(self, animation, collapsedHeight, contentHeight): animation.setDuration(500) animation.setStartValue(collapsedHeight) animation.setEndValue(collapsedHeight + contentHeight) self.animationGroup.addAnimation(animation) def onToggle(self): arrowType = Qt.RightArrow direction = QAbstractAnimation.Backward if not self.toggleButton.isChecked(): arrowType = Qt.DownArrow direction = QAbstractAnimation.Forward self.toggleButton.setArrowType(arrowType) self.animationGroup.setDirection(direction) self.animationGroup.start()
-
What do you mean by entire space ?
Can you show a picture of what you get ?
What do you expect exactly ?