Issue
So I am new to QtGui and looking up how to do things, and I found this neat example on QTreeView. When I got it working on my own, I noticed that it didn't fill the space as I'd anticipated:
So I have been searching for answers, and not finding much in either Python or C++ resources. I've been checking the documentation a lot, but still not quite finding what I'm searching for.
So it seems clear that something doesn't have the correct size policy, but I am having a hard time figuring out what. I have so far eliminated a couple of potential candidates:
- The QWidget instance holding the QTreeView instance is correctly spanning the layout it is in (the QWidget spans the width of the QGroupBox minus a little for margins).
- Since QTreeView's parent widget is the correct dimensions, I figured it's something more local to QTreeView, but when I use the setSizePolicy, none of the policies I've used seem to resolve the issue. Possibly multiple steps I'm unaware of?
- The QTreeView's inherited
viewport
(from QAbstractScrollArea is much smaller than I expect. Calling QTreeView'ssetViewport()
method with a new and empty QWidget only redraws the non-header contents background in gray instead of white, and I suspect that this is close but not where I need to look. - QTreeView has other children (besides
viewport
)that I am still investigating. - Most of what I have tried I left commented out in my code below.
This is my source code to reproduce:
import sys
from PySide.QtGui import *
class TreeTime(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.main_widget = QWidget()
self.main_layout = QVBoxLayout()
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
self.statusBar()
self.make_tree()
self.show()
def make_tree(self):
# init widgets
self.tgb = QGroupBox("[Tree Group Box Title]")
self.main_layout.addWidget(self.tgb)
tgb_layout = QVBoxLayout()
self.tgb.setLayout(tgb_layout)
tgb_widget = QWidget()
tgb_layout.addWidget(tgb_widget)
debug_btn = QPushButton("DEBUG")
tgb_layout.addWidget(debug_btn)
view = QTreeView(parent=tgb_widget)
# view.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
view.setSelectionBehavior(QAbstractItemView.SelectRows)
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['col1', 'col2', 'col3'])
view.setModel(model)
view.setUniformRowHeights(True)
# populate data
for i in range(10):
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
for j in range(3):
child1 = QStandardItem('Child {}'.format(i*3+j))
child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
parent1.appendRow([child1, child2, child3])
model.appendRow(parent1)
# span container columns
view.setFirstColumnSpanned(i, view.rootIndex(), True)
# expand third container
index = model.indexFromItem(parent1)
view.expand(index)
# select last row
selmod = view.selectionModel()
index2 = model.indexFromItem(child3)
selmod.select(index2, QItemSelectionModel.Select|QItemSelectionModel.Rows)
def print_debug_info():
print('')
for child in view.children():
print("child "+repr(child)) #not sure what all these are yet
print('')
print('self.main_widget.frameSize: '+repr(self.main_widget.frameSize()))
print('view.parent().parent().frameSize(): '+repr(view.parent().parent().frameSize())) #group box
# print('self.frameSize: '+repr(self.frameSize()))
print('self.tgb.frameSize: '+repr(self.tgb.frameSize()))
print('view.parent(): '+repr(view.parent()))
print('view.parent().frameSize(): '+repr(view.parent().frameSize()))
# print('view.parent().frameSize(): '+repr(view.parent().frameSize())+" (before)")
# print('view.parent().adjustSize(): '+repr(view.parent().adjustSize()))
# print('view.parent().frameSize(): '+repr(view.parent().frameSize())+" (after)")
print('view.viewport(): '+repr(view.viewport()))
print('view.viewport().frameSize(): '+repr(view.viewport().frameSize()))
# print('view.parent().parent().parent().frameSize(): '+repr(view.parent().parent().parent().frameSize()))
# print('calling setViewport: '+repr(view.setViewport(QWidget())))
# view.adjustSize()
debug_btn.clicked.connect(print_debug_info)
def sayHello(self):
self.statusBar().showMessage("Hello World!")
import time; time.sleep(2)
self.statusBar().showMessage("")
def sayWords(self, words):
self.statusBar().showMessage(words)
if __name__ == '__main__':
app = QApplication([])
tt = TreeTime()
sys.exit(app.exec_())
I am using a Windows 8.1 machine and Python 3.4.3, PySide version 1.2.2 - any help will be much appreciated! (also, please let me know if I left out any important details)
UPDATE (5/19/2015): I tried moving my DEBUG button outside the QGroupBox, and the result was the QTreeView being collapsed into a completely nonlegible size so you couldn't even tell what the object was anymore, so it seems to be minimizing the space used, even when I uncomment the line:
view.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
One friend has suggested this may simply be an issue with windows and not my code, but I don't have anything to back that up.
UPDATE 5/19/2015: I have implemented the advice provided by @titusjan, but I have the same problem/behavior.
Solution
You need to remove the redundant tp_widget
and add view
to tgb_layout
:
def make_tree(self):
# init widgets
self.tgb = QGroupBox("[Tree Group Box Title]")
self.main_layout.addWidget(self.tgb)
tgb_layout = QVBoxLayout()
self.tgb.setLayout(tgb_layout)
view = QTreeView()
tgb_layout.addWidget(view)
...
debug_btn = QPushButton("DEBUG")
tgb_layout.addWidget(debug_btn)
Note that when you add widgets to a layout, they will be automatically re-parented to the parent of the layout (whenever it gets one), so it's not really necessary set one in the constructor.
Also note that this:
tgb_layout = QVBoxLayout(self.tgb)
is exactly equivalent to this:
tgb_layout = QVBoxLayout()
self.tgb.setLayout(tgb_layout)
because the layout will always be re-parented to the widget it's set on.
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.