I want to use a QSplitter to split the MainWindow on two sides. On the left I want to have a layout containing a folder view and on the right another layout where I’ll plot some things. But I can only add Widgets on the QSplitter and the Layouts are not Widgets. The only similar questions I found were these:

http://www.qtcentre.org/threads/1685...le-layouts-how https://forum.qt.io/topic/8197/solve...layout-problem

So, according to the above links, my plan is to create the two layouts, then add them to two QWidgets (using setLayout) and then add these QWidgets to QSplitter (using addWidget). I tried to apply that on my code but unfortunately, when I add the layout to the widget nothing shows up!

Is there another way to achieve what I want? Have I misunderstood the answers?

Here’s the code. I send you only the folder view layout in order for the code to be simpler. Also, I’m completely new to pyqt and object-oriented programming, so any commends on how to improve it are welcome!

Thanks in advance!

Qt Code:
  1. import sys
  2. from PyQt5.QtWidgets import *
  3. from PyQt5.QtGui import *
  4. from PyQt5.QtCore import *
  5.  
  6. class Window(QMainWindow):
  7.  
  8. def __init__(self, parent=None):
  9.  
  10. super(Window, self).__init__(parent)
  11. self.UI()
  12.  
  13. def UI(self):
  14.  
  15. self.central_widget = QStackedWidget()
  16. self.setCentralWidget(self.central_widget)
  17.  
  18. self.statusBar().showMessage("Ready")
  19.  
  20. page1 = FirstWidget(self)
  21. page1.visualize_btn.clicked.connect(self.P_2)
  22. self.central_widget.addWidget(page1)
  23.  
  24. self.setGeometry(10, 10, 800, 500) #All three methods have been inherited from the QWidget class.
  25. self.showMaximized()
  26. self.setWindowTitle('PTV')
  27. self.setWindowIcon(QIcon('tuc_logo.png'))
  28.  
  29. def P_2(self):
  30.  
  31. page2 = VisualizeWidget(self)
  32. self.central_widget.addWidget(page2)
  33. self.central_widget.setCurrentWidget(page2)
  34.  
  35.  
  36. class FirstWidget(QWidget):
  37.  
  38. def __init__(self, parent=None):
  39. super(FirstWidget, self).__init__(parent)
  40. self.buttons()
  41.  
  42. def buttons(self):
  43.  
  44. self.btn2 = QPushButton("Visualize")
  45. self.buttonsLayout = QVBoxLayout()
  46. self.buttonsLayout.addWidget(self.btn2)
  47. self.visualize_btn = self.btn2
  48. self.setLayout(self.buttonsLayout)
  49.  
  50.  
  51. class VisualizeWidget(QWidget):
  52.  
  53. def __init__(self, parent=None):
  54. super(VisualizeWidget, self).__init__(parent)
  55.  
  56.  
  57. self.dirmodel = QFileSystemModel()
  58. self.dirmodel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs) # Don't show files, just folders
  59.  
  60. self.folder_view = QTreeView(parent=self);
  61. self.folder_view.setModel(self.dirmodel)
  62. self.folder_view.clicked[QModelIndex].connect(self.clicked)
  63.  
  64. self.now_layout = QVBoxLayout()
  65. self.now_layout.addWidget(self.folder_view)
  66.  
  67. #self.setLayout(self.now_layout)
  68.  
  69. #HERE is where I'm trying to add the layout to the widget.
  70. self.left_widget = QWidget()
  71. self.left_widget.setLayout(self.now_layout)
  72.  
  73.  
  74. def set_path(self):
  75. self.dirmodel.setRootPath("")
  76.  
  77. def clicked(self, index):
  78. index = self.selectionModel.currentIndex()
  79. dir_path = self.dirmodel.filePath(index)
  80.  
  81. self.filemodel.setRootPath(dir_path)
  82. self.file_view.setRootIndex(self.filemodel.index(dir_path))
  83.  
  84. def main():
  85. app = QApplication(sys.argv)
  86. win = Window()
  87. win.show()
  88. app.exec_()
  89.  
  90. if __name__ == '__main__':
  91. sys.exit(main())
To copy to clipboard, switch view to plain text mode