How to combine QMainWindows as QWidgets in QHBoxLayout in PyQt6
-
Hi everyone, I'm trying to merge the TextEdit and EditableTreeModel examples from the PyQt6 docs. My code is here: https://github.com/tsoumagas-benjamin/NoteWizard/tree/main/env, with the main files of interest being textedit.py and notetree.py for reference. I was hoping to have the layout look like the image below, but it just looks like the TextEdit example.

Both came initially as QMainWindow types, so I've tried making EditableTreeModel into a QWidget and combining it with the QTextEdit widget in the TextEdit example but I am not able to see the EditableTreeModel despite no errors occurring. These are the changes I've made to textedit.py and notetree.py
class TextEdit(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.main_layout = QHBoxLayout() self.main_layout.addWidget(NoteTree()) self.main_layout.addWidget(self._text_edit) self.layout_widget = QWidget() self.layout_widget.setLayout(self.main_layout) self.setCentralWidget(self.layout_widget)class NoteTree(QWidget): def __init__(self, parent: QWidget = None): super().__init__(parent) self.view = QTreeView() self.view.setAlternatingRowColors(True) self.view.setSelectionBehavior(QAbstractItemView.SelectItems) self.view.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel) self.view.setAnimated(False) self.view.setAllColumnsShowFocus(True) # Changes start here # self.setCentralWidget(self.view) # menubar = self.menuBar() # file_menu = menubar.addMenu("&File") # self.exit_action = file_menu.addAction("E&xit") # self.exit_action.setShortcut("Ctrl+Q") # self.exit_action.triggered.connect(self.close) # Actions to add remove files/folders # actions_menu = menubar.addMenu("&Actions") # actions_menu.triggered.connect(self.update_actions) # self.insert_row_action = actions_menu.addAction("Insert Row") # self.insert_row_action.setShortcut("Ctrl+I, R") # self.insert_row_action.triggered.connect(self.insert_row) # self.insert_column_action = actions_menu.addAction("Insert Column") # self.insert_column_action.setShortcut("Ctrl+I, C") # self.insert_column_action.triggered.connect(self.insert_column) # actions_menu.addSeparator() # self.remove_row_action = actions_menu.addAction("Remove Row") # self.remove_row_action.setShortcut("Ctrl+R, R") # self.remove_row_action.triggered.connect(self.remove_row) # self.remove_column_action = actions_menu.addAction("Remove Column") # self.remove_column_action.setShortcut("Ctrl+R, C") # self.remove_column_action.triggered.connect(self.remove_column) # actions_menu.addSeparator() # self.insert_child_action = actions_menu.addAction("Insert Child") # self.insert_child_action.setShortcut("Ctrl+N") # self.insert_child_action.triggered.connect(self.insert_child) # help_menu = menubar.addMenu("&Help") # about_qt_action = help_menu.addAction("About Qt", qApp.aboutQt) # noqa: F821 # about_qt_action.setShortcut("F1") #Changes end here self.setWindowTitle("NoteWizard") headers = ["Notes"] file = Path(__file__).parent / "default.txt" self.model = TreeModel(headers, file.read_text(), self) if "-t" in sys.argv: QAbstractItemModelTester(self.model, self) self.view.setModel(self.model) self.view.expandAll() for column in range(self.model.columnCount()): self.view.resizeColumnToContents(column) selection_model = self.view.selectionModel() # selection_model.selectionChanged.connect(self.update_actions) # self.update_actions() -
Hi everyone, I'm trying to merge the TextEdit and EditableTreeModel examples from the PyQt6 docs. My code is here: https://github.com/tsoumagas-benjamin/NoteWizard/tree/main/env, with the main files of interest being textedit.py and notetree.py for reference. I was hoping to have the layout look like the image below, but it just looks like the TextEdit example.

Both came initially as QMainWindow types, so I've tried making EditableTreeModel into a QWidget and combining it with the QTextEdit widget in the TextEdit example but I am not able to see the EditableTreeModel despite no errors occurring. These are the changes I've made to textedit.py and notetree.py
class TextEdit(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.main_layout = QHBoxLayout() self.main_layout.addWidget(NoteTree()) self.main_layout.addWidget(self._text_edit) self.layout_widget = QWidget() self.layout_widget.setLayout(self.main_layout) self.setCentralWidget(self.layout_widget)class NoteTree(QWidget): def __init__(self, parent: QWidget = None): super().__init__(parent) self.view = QTreeView() self.view.setAlternatingRowColors(True) self.view.setSelectionBehavior(QAbstractItemView.SelectItems) self.view.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel) self.view.setAnimated(False) self.view.setAllColumnsShowFocus(True) # Changes start here # self.setCentralWidget(self.view) # menubar = self.menuBar() # file_menu = menubar.addMenu("&File") # self.exit_action = file_menu.addAction("E&xit") # self.exit_action.setShortcut("Ctrl+Q") # self.exit_action.triggered.connect(self.close) # Actions to add remove files/folders # actions_menu = menubar.addMenu("&Actions") # actions_menu.triggered.connect(self.update_actions) # self.insert_row_action = actions_menu.addAction("Insert Row") # self.insert_row_action.setShortcut("Ctrl+I, R") # self.insert_row_action.triggered.connect(self.insert_row) # self.insert_column_action = actions_menu.addAction("Insert Column") # self.insert_column_action.setShortcut("Ctrl+I, C") # self.insert_column_action.triggered.connect(self.insert_column) # actions_menu.addSeparator() # self.remove_row_action = actions_menu.addAction("Remove Row") # self.remove_row_action.setShortcut("Ctrl+R, R") # self.remove_row_action.triggered.connect(self.remove_row) # self.remove_column_action = actions_menu.addAction("Remove Column") # self.remove_column_action.setShortcut("Ctrl+R, C") # self.remove_column_action.triggered.connect(self.remove_column) # actions_menu.addSeparator() # self.insert_child_action = actions_menu.addAction("Insert Child") # self.insert_child_action.setShortcut("Ctrl+N") # self.insert_child_action.triggered.connect(self.insert_child) # help_menu = menubar.addMenu("&Help") # about_qt_action = help_menu.addAction("About Qt", qApp.aboutQt) # noqa: F821 # about_qt_action.setShortcut("F1") #Changes end here self.setWindowTitle("NoteWizard") headers = ["Notes"] file = Path(__file__).parent / "default.txt" self.model = TreeModel(headers, file.read_text(), self) if "-t" in sys.argv: QAbstractItemModelTester(self.model, self) self.view.setModel(self.model) self.view.expandAll() for column in range(self.model.columnCount()): self.view.resizeColumnToContents(column) selection_model = self.view.selectionModel() # selection_model.selectionChanged.connect(self.update_actions) # self.update_actions()@BTsoumagas
In your codeself.view = QTreeView()creates a treeview but you never actually add/show it in the widget. Either you need to put layout on the widget and add the treeview to that, or you don't want/need aQWidgetwrapper around the tree view at all, just use it directly or makeNoteTreederive fromQTreeView. -
B BTsoumagas has marked this topic as solved on