Issue
In the following code, the QTreeView widget always takes up maximum space and leaves minimum space for the QPushButton widget (images after the code). If the QTreeView widget is replaced with an another QPushButton (when code #1 is commented out, and code #2 is commented in), then both buttons get an equal amount of screen space. If QPushButton widgets are replaced with two QTreeView widgets, then space is divided equally between two widgets as well.
My question is why is QTreeView favoured, regarding screen space, over other widgets like QPushButton and how can I avoid it (how to divide space equally between the two widgets)?
So far, I have discovered, that the only functions which work are setFixedSize or setFixedWidth, which both are non-preferred solutions.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QTreeView, QPushButton, QHBoxLayout
class TestClass(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_window = QMainWindow()
self.main_widget = QWidget()
self.main_layout = QHBoxLayout()
self.random_tree = QTreeView() # <--- Code #1
self.random_button = QPushButton('Test Button')
# self.random_button_2 = QPushButton('Test Button') # <--- Code #2
self.main_layout.addWidget(self.random_tree) # <--- Code #1
self.main_layout.addWidget(self.random_button)
# self.main_layout.addWidget(self.random_button_2) # <--- Code #2
self.main_widget.setLayout(self.main_layout)
self.main_window.setCentralWidget(self.main_widget)
def show(self):
self.main_window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TestClass()
window.show()
sys.exit(app.exec_())
Here are the pictures, which describes the situation:
QTreeView being selfish
QPushButton widgets sharing the screen in a friendly manner
Solution
By default the stretch factor of the items added to a layout is 0, and in that case the layout will take into account the QSizePolicy of each widget to calculate the width of each widget, in this case it states that the QTreeView will expand.
The horizontalPolicy(widget.sizePolicy().horizontalPolicy()
) of each widget are:
QPushButton : QSizePolicy::Minimum
QTreeView : QSizePolicy::Expanding
The solution is to set the same stretch factor by adding each widget.
import sys
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QMainWindow,
QTreeView,
QPushButton,
QHBoxLayout,
)
class TestClass(QMainWindow):
def __init__(self, parent=None):
super(TestClass, self).__init__(parent)
self.random_tree = QTreeView()
self.random_button = QPushButton("Test Button")
self.main_widget = QWidget()
main_layout = QHBoxLayout(self.main_widget)
main_layout.addWidget(self.random_tree, stretch=1)
main_layout.addWidget(self.random_button, stretch=1)
self.setCentralWidget(self.main_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TestClass()
window.show()
sys.exit(app.exec_())
Note: You are creating a class that inherits from QMainWidow but you never show it, which seems at least odd to me, so restructure its code.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.