Issue
I created a QScrollArea
to show a directory tree and a file tree. When directories or files are shown in this area, the vertical scrollbar appears, but the horizontal scrollbar never works. This is the code (the actual code is very large, so I'm showing only relevant portions):
class SomeWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
def DirectoryOrFileSelection(self):
layoutOne = QVBoxLayout()
self.treeview_tabs = QTabWidget()
self.directoryView = QScrollArea(widgetResizable=True) #QWidget()
self.directoryView.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.filesView = QScrollArea(widgetResizable=True) #QWidget()
self.filesView.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.treeview_tabs.addTab(self.directoryView, "Dirs")
self.treeview_tabs.addTab(self.filesView, "Files")
theHBoxLayout = QHBoxLayout()
leftSpacing = 4
theHBoxLayout.addWidget(self.treeview_tabs, leftSpacing)
layoutOne.addLayout(theHBoxLayout)
self.layout.addLayout(layoutOne)
This is what it looks like when there's no need for a scrollbar.
The vertical scrollbar promptly appears when I make the main window smaller.
However, when I expand one of the directories, even though the filenames go beyond the visible area, the horizontal scrollbar does not appear.
If I use Qt.ScrollBarAlwaysOn
in place of Qt.ScrollBarAsNeeded
, an inactive scrollbar shows up and never becomes active.
Could anyone help with how to make the horizontal scrollbar active when necessary? I need to be able to scroll horizontally to see the full filenames.
UPDATE: As per suggestions received, I applied the setHorizontalScrollBarPolicy
directly to the QTreeView
, but even though the scrollbar appears and looks like it's active, it does not seem to recognize when the content is going outside the view area. Shown in the image below.
Solution
There is no need to add the view to another scroll area, as all Qt item views are scroll areas.
Note that, unlike the headers of QTableView, the header of QTreeView automatically stretches the last section:
Note: The horizontal headers provided by QTreeView are configured with this property set to true, ensuring that the view does not waste any of the space assigned to it for its header. If this value is set to true, this property will override the resize mode set on the last section in the header.
This means that if you are only showing the first column of the tree, the names of files and directories will always be elided if their width exceeds the width of the widget, and the horizontal scroll bar will not be activated even if it's always shown.
treeview.header().setStretchLastSection(False)
treeview.header().setSectionResizeMode(QHeaderView.ResizeToContents)
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.