Solved How to make a QTreeView display more than 10 lines
-
I am working on getting a QTreeView that uses a QStandardItemModel to display more than 10 lines. I want it to show the maximum number of lines the area can actually handle but it stops short at 10 for some unknown (by me at least) reason.
I have tried numerous solutions (some I have remarked out within the code below along with their apparent results) and there are others that I am not including because they did not compile. As of yet I cannot seem to find a way to get the QTreeView to fill the available space and/or show all the lines it can it just stops at 10 lines. I am including a program that runs and shows the issue simply click "Options" then click "Option 1"
Again what I had expected is that the QTreeView would fill the available space and/or show as many rows as possible within the given space. However, as stated it stops short and only displays just 10 lines maximum.
from sys import exit as sysExit from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Line(QFrame): def __init__(self, parent): QFrame.__init__(self) self.setFrameShape(QFrame.HLine) self.setFrameShadow(QFrame.Sunken) # Users Widget ************************************************* class Disply1(QFrame): def __init__(self, parent): QFrame.__init__(self) self.setFrameShape(QFrame.StyledPanel) self.setLineWidth(0.2) # ------- self.header = QVBoxLayout() Title = QLabel() Title.setStyleSheet('font: bold 14px') Title.setText('Option 1 Configuration Settings') self.header.addWidget(Title) self.header.addWidget(Line(self)) # ------- self.TreeVew = QTreeView() self.TreeVew.setEditTriggers(QAbstractItemView.NoEditTriggers) self.TreeVew.setAlternatingRowColors(True) # This adjustment did not fix it # self.TreeVew.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) # This adjustment made it worse # self.TreeVew.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) self.model = QStandardItemModel(0, 6) self.model.setHorizontalHeaderLabels([' ', 'Column1', 'Column2', 'Column3', 'Column4', 'Column5']) self.model.setHeaderData(5, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole) # This adjustment did not fix it # self.model.setRowCount(20) self.TreeVew.setModel(self.model) self.TreeVew.setColumnWidth(0, 1) self.TreeVew.setColumnHidden(0, True) self.TreeVew.header().setSectionResizeMode(0, QHeaderView.Fixed) self.TreeVew.header().setStretchLastSection(False) self.TreeVew.header().setSectionResizeMode(1, QHeaderView.Stretch) self.TreeVew.setColumnWidth(2, 100) self.TreeVew.setColumnWidth(3, 100) self.TreeVew.setColumnWidth(4, 100) self.TreeVew.setColumnWidth(5, 70) # This adjustment did not fix it # self.TreeVew.resize(self.sizeHint().width(), self.maximumHeight()) ItmRecSet = [ {'Id':'01', 'Column1':'R01-C1', 'Column2':'R01-C2', 'Column3':'R01-C3', 'Column4':'R01-C5', 'Column5':'R01-C5'}, {'Id':'02', 'Column1':'R02-C1', 'Column2':'R02-C2', 'Column3':'R02-C3', 'Column4':'R02-C5', 'Column5':'R02-C5'}, {'Id':'03', 'Column1':'R03-C1', 'Column2':'R03-C2', 'Column3':'R03-C3', 'Column4':'R03-C5', 'Column5':'R03-C5'}, {'Id':'04', 'Column1':'R04-C1', 'Column2':'R04-C2', 'Column3':'R04-C3', 'Column4':'R04-C5', 'Column5':'R04-C5'}, {'Id':'05', 'Column1':'R05-C1', 'Column2':'R05-C2', 'Column3':'R05-C3', 'Column4':'R05-C5', 'Column5':'R05-C5'}, {'Id':'06', 'Column1':'R06-C1', 'Column2':'R06-C2', 'Column3':'R06-C3', 'Column4':'R06-C5', 'Column5':'R06-C5'}, {'Id':'07', 'Column1':'R07-C1', 'Column2':'R07-C2', 'Column3':'R07-C3', 'Column4':'R07-C5', 'Column5':'R07-C5'}, {'Id':'08', 'Column1':'R08-C1', 'Column2':'R08-C2', 'Column3':'R08-C3', 'Column4':'R08-C5', 'Column5':'R08-C5'}, {'Id':'09', 'Column1':'R09-C1', 'Column2':'R09-C2', 'Column3':'R09-C3', 'Column4':'R09-C5', 'Column5':'R09-C5'}, {'Id':'10', 'Column1':'R10-C1', 'Column2':'R10-C2', 'Column3':'R10-C3', 'Column4':'R10-C5', 'Column5':'R10-C5'}, {'Id':'11', 'Column1':'R11-C1', 'Column2':'R11-C2', 'Column3':'R11-C3', 'Column4':'R11-C5', 'Column5':'R11-C5'}, {'Id':'12', 'Column1':'R12-C1', 'Column2':'R12-C2', 'Column3':'R12-C3', 'Column4':'R12-C5', 'Column5':'R12-C5'}, {'Id':'13', 'Column1':'R13-C1', 'Column2':'R13-C2', 'Column3':'R13-C3', 'Column4':'R13-C5', 'Column5':'R13-C5'}, {'Id':'14', 'Column1':'R14-C1', 'Column2':'R14-C2', 'Column3':'R14-C3', 'Column4':'R14-C5', 'Column5':'R14-C5'}, {'Id':'15', 'Column1':'R15-C1', 'Column2':'R15-C2', 'Column3':'R15-C3', 'Column4':'R15-C5', 'Column5':'R15-C5'}, {'Id':'16', 'Column1':'R16-C1', 'Column2':'R16-C2', 'Column3':'R16-C3', 'Column4':'R16-C5', 'Column5':'R16-C5'}, {'Id':'17', 'Column1':'R17-C1', 'Column2':'R17-C2', 'Column3':'R17-C3', 'Column4':'R17-C5', 'Column5':'R17-C5'}, {'Id':'18', 'Column1':'R18-C1', 'Column2':'R18-C2', 'Column3':'R18-C3', 'Column4':'R18-C5', 'Column5':'R18-C5'}, {'Id':'19', 'Column1':'R19-C1', 'Column2':'R19-C2', 'Column3':'R19-C3', 'Column4':'R19-C5', 'Column5':'R19-C5'}, {'Id':'20', 'Column1':'R20-C1', 'Column2':'R20-C2', 'Column3':'R20-C3', 'Column4':'R20-C5', 'Column5':'R20-C5'} ] self.model.setRowCount(0) for Item in ItmRecSet: Itm1 = QStandardItem(Item['Id']) Itm1.setTextAlignment(Qt.AlignLeft) Itm1.isEditable = False Itm2 = QStandardItem(Item['Column1']) Itm2.setTextAlignment(Qt.AlignLeft) Itm2.isEditable = False Itm3 = QStandardItem(Item['Column2']) Itm3.setTextAlignment(Qt.AlignLeft) Itm3.isEditable = False Itm4 = QStandardItem(Item['Column3']) Itm4.setTextAlignment(Qt.AlignLeft) Itm4.isEditable = False Itm5 = QStandardItem(Item['Column4']) Itm5.setTextAlignment(Qt.AlignLeft) Itm5.isEditable = False Itm6 = QStandardItem(Item['Column5']) Itm6.setTextAlignment(Qt.AlignCenter) Itm6.isEditable = False self.model.appendRow([Itm1, Itm2, Itm3, Itm4, Itm5, Itm6]) # Using this GridLayout versus the VerticalBox did not fix it # self.Cntnr = QGridLayout() # ------- # self.Cntnr.addLayout(self.header,0,0) # self.Cntnr.addWidget(self.TreeVew,1,0) # self.Cntnr.setRowStretch(2,4) self.Cntnr = QVBoxLayout() self.Cntnr.addLayout(self.header) self.Cntnr.addWidget(self.TreeVew) # If this stretch is not included only the header gets stretched the QTreeView still only shows 10 lines self.Cntnr.addStretch(1) # ------- self.setLayout(self.Cntnr) # Debug Widget ************************************************* class Disply2(QFrame): def __init__(self, parent): QFrame.__init__(self) self.setFrameShape(QFrame.StyledPanel) self.setLineWidth(0.2) # ------- self.Cntnr = QVBoxLayout() self.Cntnr.addWidget(QTextEdit('Option 2 Settings Shown Here')) self.Cntnr.addStretch(1) # ------- self.setLayout(self.Cntnr) class OptionButtons(QToolButton): # Class OptionButtons ("Text", Icon, Connector) inherits from QToolButton def __init__(self, Text, Icon, Connector): QToolButton.__init__(self) self.setText(Text) self.setIcon(Icon) self.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) self.setStyleSheet("font: bold;color: blue;height: 55px;width: 55px;") self.setIconSize(QSize(32,32)) self.clicked.connect(Connector) ############################## Settings Class ############################## class OptionSettings(QDialog): def __init__(self, parent): QDialog.__init__(self, parent) line = QFrame() line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) # Button Area on Left # Button 1 ************************************************* self.btnOptn1 = OptionButtons('Option1', QIcon('images/opt1.ico'), self.ShowDisply1) # Button 2 ************************************************* self.btnOptn2 = OptionButtons('Option2', QIcon('images/opt2.ico'), self.ShowDisply2) # Vertical Box for Buttons ************************************* self.UpLeft = QVBoxLayout() self.UpLeft.addWidget(self.btnOptn1) self.UpLeft.addWidget(self.btnOptn2) self.UpLeft.addStretch(1) # Display Area on Right # Widget Flip Display ****************************************** self.UpRite = QHBoxLayout() self.Contents = QStackedWidget() self.Contents.addWidget(QTextEdit('Nothing Selected')) self.Contents.addWidget(Disply1(self)) self.Contents.addWidget(Disply2(self)) self.Contents.addWidget(QTextEdit('Settings Saved')) self.Contents.setCurrentIndex(0) self.UpRite.addWidget(self.Contents) # Button and Display Area on Top self.Upper = QHBoxLayout() self.Upper.addLayout(self.UpLeft) self.Upper.addLayout(self.UpRite) # Save and Cancel Area on Bottom self.btnSave = QPushButton("Save") self.btnSave.clicked.connect(self.SaveSettings) self.btnCncl = QPushButton("Cancel") self.btnCncl.clicked.connect(self.close) self.Lower = QHBoxLayout() self.Lower.addStretch(1) self.Lower.addWidget(self.btnSave) self.Lower.addWidget(self.btnCncl) # Entire Options Window Layout self.OuterBox = QVBoxLayout() self.OuterBox.addLayout(self.Upper) self.OuterBox.addLayout(self.Lower) self.setLayout(self.OuterBox) self.setWindowTitle('Settings') #Geometry(Left, Top, Width, Hight) self.setGeometry(250, 250, 550, 450) self.setModal(True) self.exec() def ShowDisply1(self): self.Contents.setCurrentIndex(1) def ShowDisply2(self): self.Contents.setCurrentIndex(2) def SaveSettings(self): self.Contents.setCurrentIndex(3) class CustomItemModel(QStandardItemModel): def headerData(self, section, orientation, role): if role == Qt.ForegroundRole: brush = QBrush() brush.setColor(Qt.blue) brush.setStyle(Qt.SolidPattern) return brush elif role == Qt.BackgroundRole: brush = QBrush() brush.setColor(Qt.yellow) brush.setStyle(Qt.SolidPattern) return brush elif role == Qt.FontRole: font = QFont() font.setBold(True) font.setPointSize(10) return font return super().headerData(section, orientation, role) class ItemDsplyr(QTreeView): def __init__(self, CentrPane): QTreeView.__init__(self, CentrPane) self.CntrPane = CentrPane self.setEditTriggers(QTreeView().NoEditTriggers) self.model = CustomItemModel(0, 3) self.model.setHorizontalHeaderLabels(['1st Col', '2nd Col', '3rd Col']) self.model.setHeaderData(1, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole) self.setModel(self.model) self.setMinimumWidth(250) self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) self.header().setStretchLastSection(False) self.header().setSectionResizeMode(0, QHeaderView.Stretch) self.setColumnWidth(1, 75) self.setColumnWidth(2, 100) class CenterPanel(QWidget): def __init__(self, MainWin): QWidget.__init__(self) self.MyEditor = QTextEdit('Editorial') self.ItemDsply = ItemDsplyr(self) CntrPane = QSplitter(Qt.Horizontal, self) CntrPane.addWidget(self.MyEditor) CntrPane.addWidget(self.ItemDsply) CntrPane.setSizes([50,200]) CntrPane.setCollapsible(0, False) CntrPane.setCollapsible(1, False) hbox = QHBoxLayout(self) hbox.addWidget(CntrPane) self.setLayout(hbox) class MenuToolBar(QDockWidget): def __init__(self, MainWin): QDockWidget.__init__(self) self.MainWin = MainWin self.MainMenu = MainWin.menuBar() self.WndowMenu = self.MainMenu.addMenu('Windows') self.OptnAct = QAction('Options', self) self.OptnAct.setStatusTip('Open the Options Window') self.OptnAct.triggered.connect(MainWin.ShowOptions) self.WndowMenu.addAction(self.OptnAct) self.InitToolBar(MainWin) def InitToolBar(self, MainWin): self.mainToolBar = MainWin.addToolBar("Quick Access") self.mainToolBar.addAction(self.OptnAct) class UI_MainWindow(QMainWindow): def __init__(self, MainDesktop): super(UI_MainWindow, self).__init__(MainDesktop) self.setWindowTitle('Main Window') self.MnDskTop = MainDesktop # Left, Top, Width, Height self.setGeometry(200, 200, 550, 550) self.CenterPane = CenterPanel(self) self.setCentralWidget(self.CenterPane) self.MenuToolBar = MenuToolBar(self) def ShowOptions(self): self.Options = OptionSettings(self) if __name__ == '__main__': MainApp = QApplication([]) MainGui = UI_MainWindow(MainApp.desktop()) MainGui.show() sysExit(MainApp.exec_())
-
Okay perhaps I unknowingly adjusted something somewhere but now all that has to be done to get this to work properly is to do something I had already tested a few times but did not work when I tested it then.
Remove the line self.Cntnr.addStretch(1), that's where the issue resides
Note: The answer to this question was received elsewhere just sharing what that answer ended up being.