我想使用QSplitter在两侧分割MainWindow。在左边我想要一个包含文件夹视图的布局,在右边另一个布局我将绘制一些东西。但我只能在QSplitter上添加Widgets,而Layouts不是Widgets。 我发现的唯一类似问题是:
http://www.qtcentre.org/threads/16856-QSplitter-multiple-layouts-how https://forum.qt.io/topic/8197/solved-qsplitter-and-qvboxlayout-problem
所以,根据上面的链接,我的计划是创建两个布局,然后将它们添加到两个QWidgets(使用setLayout),然后将这些QWidgets添加到QSplitter(使用addWidget)。 我尝试将其应用于我的代码,但不幸的是,当我向窗口小部件添加布局时,没有任何显示!
还有另一种方法可以达到我的目的吗? 我误解了答案吗?
这是代码。我只向您发送文件夹视图布局,以便使代码更简单。 另外,我对pyqt和面向对象编程完全陌生,所以欢迎任何关于如何改进它的赞誉!
提前致谢!
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.UI()
def UI(self):
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.statusBar().showMessage("Ready")
page1 = FirstWidget(self)
page1.visualize_btn.clicked.connect(self.P_2)
self.central_widget.addWidget(page1)
self.setGeometry(10, 10, 800, 500) #All three methods have been inherited from the QWidget class.
self.showMaximized()
self.setWindowTitle('PTV')
self.setWindowIcon(QIcon('tuc_logo.png'))
def P_2(self):
page2 = VisualizeWidget(self)
self.central_widget.addWidget(page2)
self.central_widget.setCurrentWidget(page2)
class FirstWidget(QWidget):
def __init__(self, parent=None):
super(FirstWidget, self).__init__(parent)
self.buttons()
def buttons(self):
self.btn2 = QPushButton("Visualize")
self.buttonsLayout = QVBoxLayout()
self.buttonsLayout.addWidget(self.btn2)
self.visualize_btn = self.btn2
self.setLayout(self.buttonsLayout)
class VisualizeWidget(QWidget):
def __init__(self, parent=None):
super(VisualizeWidget, self).__init__(parent)
self.dirmodel = QFileSystemModel()
self.dirmodel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs) # Don't show files, just folders
self.folder_view = QTreeView(parent=self);
self.folder_view.setModel(self.dirmodel)
self.folder_view.clicked[QModelIndex].connect(self.clicked)
self.now_layout = QVBoxLayout()
self.now_layout.addWidget(self.folder_view)
#self.setLayout(self.now_layout)
#HERE is where I'm trying to add the layout to the widget.
self.left_widget = QWidget()
self.left_widget.setLayout(self.now_layout)
def set_path(self):
self.dirmodel.setRootPath("")
def clicked(self, index):
index = self.selectionModel.currentIndex()
dir_path = self.dirmodel.filePath(index)
self.filemodel.setRootPath(dir_path)
self.file_view.setRootIndex(self.filemodel.index(dir_path))
def main():
app = QApplication(sys.argv)
win = Window()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())
答案 0 :(得分:0)
我实际上还没有理解我做错了什么,但我发布了一个有效的代码,以防将来帮助其他人。
现在使用QSplitter分割两个布局。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.UI()
def UI(self):
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.statusBar().showMessage("Ready")
page1 = FirstWidget(self)
page1.visualize_btn.clicked.connect(self.P_2)
self.central_widget.addWidget(page1)
self.setGeometry(10, 10, 800, 500) #All three methods have been inherited from the QWidget class.
self.showMaximized()
self.setWindowTitle('PTV')
self.setWindowIcon(QIcon('tuc_logo.png'))
def P_2(self):
page2 = VisualizeWidget(self)
self.central_widget.addWidget(page2)
self.central_widget.setCurrentWidget(page2)
class FirstWidget(QWidget):
def __init__(self, parent=None):
super(FirstWidget, self).__init__(parent)
self.buttons()
def buttons(self):
self.btn2 = QPushButton("Visualize")
self.buttonsLayout = QVBoxLayout()
self.buttonsLayout.addWidget(self.btn2)
self.visualize_btn = self.btn2
self.setLayout(self.buttonsLayout)
class VisualizeWidget(QWidget):
def __init__(self, parent=None):
super(VisualizeWidget, self).__init__(parent)
#Creation of the left layout
self.dirmodel = QFileSystemModel()
self.dirmodel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs) # Don't show files, just folders
self.folder_view = QTreeView(parent=self);
self.folder_view.setModel(self.dirmodel)
self.folder_view.clicked[QModelIndex].connect(self.clicked)
self.selectionModel = self.folder_view.selectionModel()
self.now_layout = QVBoxLayout()
self.now_layout.addWidget(self.folder_view)
self.left_widget = QWidget()
self.left_widget.setLayout(self.now_layout)
#Creation of the right layout
#self.right_widget = QTextEdit();
self.btn1 = QPushButton('btn1')
self.right_layout = QVBoxLayout()
self.right_layout.addWidget(self.btn1)
self.right_widget = QWidget()
self.right_widget.setLayout(self.right_layout)
splitter_filebrowser = QSplitter(Qt.Horizontal)
splitter_filebrowser.addWidget(self.left_widget)
splitter_filebrowser.addWidget(self.right_widget)
splitter_filebrowser.setStretchFactor(1, 1)
hbox = QHBoxLayout(self)
hbox.addWidget(splitter_filebrowser)
self.setLayout(hbox)
def set_path(self):
self.dirmodel.setRootPath("")
def clicked(self, index):
index = self.selectionModel.currentIndex()
dir_path = self.dirmodel.filePath(index)
self.filemodel.setRootPath(dir_path)
self.file_view.setRootIndex(self.filemodel.index(dir_path))
def main():
app = QApplication(sys.argv)
win = Window()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())
答案 1 :(得分:0)
我自由地清理@ A.Dew对其最小通用形式的回答。
left.merge(right2, left_on='idxkey', right_on='colkey')
value_x colkey value_y
0 -0.402655 B 0.543843
1 -0.524349 D 0.013135