Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to combine QMainWindows as QWidgets in QHBoxLayout in PyQt6

How to combine QMainWindows as QWidgets in QHBoxLayout in PyQt6

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 346 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BTsoumagas
    wrote on last edited by
    #1

    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.
    f2b28b93-1700-4eb2-bb9d-e1eb589b64fb-image.png

    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()
    
    JonBJ 1 Reply Last reply
    0
    • B BTsoumagas

      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.
      f2b28b93-1700-4eb2-bb9d-e1eb589b64fb-image.png

      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()
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @BTsoumagas
      In your code self.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 a QWidget wrapper around the tree view at all, just use it directly or make NoteTree derive from QTreeView.

      1 Reply Last reply
      2
      • B BTsoumagas has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved