Issue
I want to add multiple buttons and a label inside the header of a tree widget.
This is what I want : wireframe.
This is what I tried so far :
from PySide2 import QtWidgets, QtCore
import sys
class Header(QtWidgets.QHeaderView):
def __init__(self, orientation, parent=None):
super(Header, self).__init__(orientation, parent)
self.layout_header = QtWidgets.QHBoxLayout()
self.button = QtWidgets.QPushButton('Button text')
self.button_2 = QtWidgets.QPushButton('Button text')
self.label = QtWidgets.QLabel('label')
self.layout_header.addWidget(self.button)
self.layout_header.addWidget(self.button_2)
self.layout_header.addWidget(self.label)
self.setLayout(self.layout_header)
class TreeWidget(QtWidgets.QTreeWidget):
def __init__(self, parent=None):
super(TreeWidget, self).__init__(parent)
item0 = QtWidgets.QTreeWidgetItem(["Item 0"])
item1 = QtWidgets.QTreeWidgetItem(["Item 1"])
self.addTopLevelItem(item0)
self.addTopLevelItem(item1)
self.createHeader()
def createHeader(self):
header = Header(QtCore.Qt.Horizontal, self)
self.setHeader(header)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = TreeWidget()
w.show()
sys.exit(app.exec_())
I don't have any errors, but it doesn't work as well. Is it even possible to do that ?
Thank you
Solution
As musicamante said, I needed to declare a minimum height in order to see it.
answer :
from PySide6 import QtWidgets, QtCore
import sys
class Header(QtWidgets.QHeaderView):
def __init__(self, orientation, parent=None):
super(Header, self).__init__(orientation, parent)
self.layout_header = QtWidgets.QHBoxLayout()
self.button = QtWidgets.QPushButton('Button text')
self.button_2 = QtWidgets.QPushButton('Button text')
self.label = QtWidgets.QLabel('label')
self.layout_header.addWidget(self.button)
self.layout_header.addWidget(self.button_2)
self.layout_header.addWidget(self.label)
self.setMinimumHeight(50)
self.setLayout(self.layout_header)
class TreeWidget(QtWidgets.QTreeWidget):
def __init__(self, parent=None):
super(TreeWidget, self).__init__(parent)
item0 = QtWidgets.QTreeWidgetItem(["Item 0"])
item1 = QtWidgets.QTreeWidgetItem(["Item 1"])
self.addTopLevelItem(item0)
self.addTopLevelItem(item1)
self.createHeader()
def createHeader(self):
header = Header(QtCore.Qt.Horizontal, self)
self.setHeader(header)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = TreeWidget()
w.show()
sys.exit(app.exec_())
Answered By - isaac
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.