+ Reply to Thread
Results 1 to 2 of 2

Thread: PyQt - add layouts to QSplitter

  1. #1
    Join Date
    Mar 2017
    Posts
    4
    Qt products
    Platforms
    Windows

    Default PyQt - add layouts to QSplitter

    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 

  2. #2
    Join Date
    Mar 2017
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: PyQt - add layouts to QSplitter

    I haven't actually understood yet what I was doing wrong, but I'm posting a working code, in case it helps someone else in the future.

    Now the two layouts are splitted with a QSplitter.

    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. #Creation of the left layout
    58. self.dirmodel = QFileSystemModel()
    59. self.dirmodel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs) # Don't show files, just folders
    60.  
    61. self.folder_view = QTreeView(parent=self);
    62. self.folder_view.setModel(self.dirmodel)
    63. self.folder_view.clicked[QModelIndex].connect(self.clicked)
    64.  
    65. self.selectionModel = self.folder_view.selectionModel()
    66.  
    67. self.now_layout = QVBoxLayout()
    68. self.now_layout.addWidget(self.folder_view)
    69.  
    70.  
    71. self.left_widget = QWidget()
    72. self.left_widget.setLayout(self.now_layout)
    73.  
    74.  
    75. #Creation of the right layout
    76. #self.right_widget = QTextEdit();
    77.  
    78. self.btn1 = QPushButton('btn1')
    79.  
    80. self.right_layout = QVBoxLayout()
    81. self.right_layout.addWidget(self.btn1)
    82.  
    83. self.right_widget = QWidget()
    84. self.right_widget.setLayout(self.right_layout)
    85.  
    86.  
    87. splitter_filebrowser = QSplitter(Qt.Horizontal)
    88. splitter_filebrowser.addWidget(self.left_widget)
    89. splitter_filebrowser.addWidget(self.right_widget)
    90. splitter_filebrowser.setStretchFactor(1, 1)
    91.  
    92. hbox = QHBoxLayout(self)
    93. hbox.addWidget(splitter_filebrowser)
    94.  
    95. self.setLayout(hbox)
    96.  
    97.  
    98. def set_path(self):
    99. self.dirmodel.setRootPath("")
    100.  
    101. def clicked(self, index):
    102. index = self.selectionModel.currentIndex()
    103. dir_path = self.dirmodel.filePath(index)
    104.  
    105. self.filemodel.setRootPath(dir_path)
    106. self.file_view.setRootIndex(self.filemodel.index(dir_path))
    107.  
    108. def main():
    109. app = QApplication(sys.argv)
    110. win = Window()
    111. win.show()
    112. app.exec_()
    113.  
    114. if __name__ == '__main__':
    115. sys.exit(main())
    To copy to clipboard, switch view to plain text mode 

+ Reply to Thread

Similar Threads

  1. Replies: 0
    Last Post: 29th January 2015, 11:55
  2. Upgrading from PyQt 4.5 to PyQt 4.7
    By RogerCon in forum Installation and Deployment
    Replies: 0
    Last Post: 14th July 2010, 19:52
  3. QSplitter + multiple layouts? how?
    By jalla2000 in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2008, 14:52
  4. Layouts - Name cant be set
    By manivannan_1984 in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2006, 19:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts