-1

i want to display two QTreeViews inside one window and i cant figure out why my testing code doesn't show the red widget (future 2nd TreeView). Any ideas why it doesn't appear? I am new to PyQt5 and ive followed a tutorial on youtube and a written here. Before i started this question ive searched on stackoverflow, but i didn't find a topic which had this issue. StandardItem is a subclass of QStandardItem and Color is a subclass of QWidget. Im not defining any layouts inside both classes (just setting default settings for QStandardItems and adding color to see my layout).

class MainWindow(QMainWindow):

def __init__(self):
    super(MainWindow, self).__init__()

    self.setWindowTitle("JSON View")
    self.resize(700, 700)

    treeView = QTreeView(self)
    treeView.setHeaderHidden(True)  # To hide first column
    
    treeModel = QStandardItemModel()  
    rootNode = treeModel.invisibleRootItem()
    
    # Data
    america = StandardItem('America', 16, set_bold=True)
    california = StandardItem('California', 14)
    america.appendRow(california)
    oakland = StandardItem('Oakland', 12)
    california.appendRow(oakland)
    rootNode.appendRow(america)
    treeView.setModel(treeModel)
    treeView.expandAll()
    treeView.doubleClicked.connect(self.getValue)
    
    # Layout
    layout = QHBoxLayout()
    layout.setContentsMargins(0, 0, 0, 0)
    layout.setSpacing(20)
    layout.addWidget(Color('red'))
    layout.addWidget(Color('yellow'))
    layout.addWidget(treeView)
    treeView.setVisible(True)
    widget = QWidget()
    widget.setLayout(layout)
    self.setCentralWidget(widget)
musicamante
  • 41,230
  • 6
  • 33
  • 58
Tom
  • 37
  • 8
  • It seems like the widgets i add to the horizontal layout get overriden by the TreeView when i add it. I have no idea what im missing here – Tom Jan 29 '23 at 09:42
  • Please provide a [mre]: you included lot of code that's completely irrelevant to the question (the model and the custom item) and left out the most important one, the `Color` class. Besides, why don't you just add the second tree view instead? – musicamante Jan 29 '23 at 09:43
  • Done. I dont have one defined yet because i cant even get the layout right. When the layout works ill define the second tree view and use it instead of the Color instances. – Tom Jan 29 '23 at 09:51
  • No, that is *not* a MRE. Your problem is related to a `Color` class, how can we **reproduce** it if you don't show us its code? That said, there is no point in using a *different* widget type as a placeholder: widgets have very different behavior about sizes, depending on their capabilities. It doesn't matter if the other tree view has no model or its still empty, if you want to get the layout right, use *that* widget, not another one. Just replace those Color instances with QTreeView ones and you'll see. – musicamante Jan 29 '23 at 10:00

1 Answers1

0

So after reading more documentation (QTreeView, QGroupBox, QVBoxLayout) on this topic ive found that i had to use an additional layout inside where i can combine two more layouts for each tree view. Afterwards i would use a dummy-widget as central widget. I posted the code below if anyone has the same problem:

class MainWindow(QMainWindow):
def __init__(self):
    super(MainWindow, self).__init__()
    self.setWindowTitle("JSON View")
    # Retrieve geometry of window
    windowGeometry = self.frameGeometry()
    # Get center of the screen
    centerScreen = QDesktopWidget().availableGeometry().center()
    # Move window to the center
    windowGeometry.moveCenter(centerScreen)
    self.move(windowGeometry.topLeft())

    treeViewLeft = QTreeView(self)
    treeViewLeft.setHeaderHidden(True)  # To hide first column
    treeViewRight = QTreeView(self)
    treeViewRight.setHeaderHidden(True)

    groupBoxLeft = QGroupBox('1. Json tree view')
    groupBoxRight = QGroupBox('2. Json tree view')

    # TODO: Add Column headers for each tree view
    treeModelLeft = QStandardItemModel(0, 7)  
    rootNodeLeft = treeModelLeft.invisibleRootItem()  # Provides access to the model’s top-level items through the QStandardItem API
    treeModelRight = QStandardItemModel()  
    rootNodeRight = treeModelRight.invisibleRootItem()  # Provides access to the model’s top-level items through the QStandardItem API
    
    # Data (left)
    america = StandardItem('America', 16, set_bold=True)
    california = StandardItem('California', 14)
    america.appendRow(california)
    oakland = StandardItem('Oakland', 12)
    california.appendRow(oakland)
    rootNodeLeft.appendRow(america)
    treeViewLeft.setModel(treeModelLeft)
    treeViewLeft.expandAll()
    treeViewLeft.doubleClicked.connect(self.getValue)
    treeViewLeft.setVisible(True)
    
    # Data (right)
    america = StandardItem('America', 16, set_bold=True)
    california = StandardItem('California', 14)
    america.appendRow(california)
    oakland = StandardItem('Oakland', 12)
    california.appendRow(oakland)
    rootNodeRight.appendRow(america)
    treeViewRight.setModel(treeModelRight)
    treeViewRight.expandAll()
    treeViewRight.doubleClicked.connect(self.getValue)
    treeViewRight.setVisible(True)

    # Layout
    hbox = QVBoxLayout()
    hbox.addWidget(treeViewLeft)
    groupBoxLeft.setLayout(hbox)

    hbox2 = QVBoxLayout()
    hbox2.addWidget(treeViewRight)
    groupBoxRight.setLayout(hbox2)

    mainLayout = QHBoxLayout()
    mainLayout.addWidget(groupBoxLeft)
    mainLayout.addWidget(groupBoxRight)

    widget = QWidget()  # Dummy
    widget.setLayout(mainLayout)
    self.setCentralWidget(widget)  # Must-have


def getValue(self, val):
    print(val.data())
    print(val.row())
    print(val.column())

The following code-examples helped aswell: pythonguis and pythonspot

Cheers!

Tom
  • 37
  • 8