如何调整大小并将 QGridLayout 动态移动到 1/4s
How to resize and move QGridLayout dynamically to 1/4s
这是我当前的 window 布局,我想将它分成 1/4,而不是 1/2 可以这么说。
这就是我希望我的 window 看起来的样子,但我似乎找不到一个好的方法来将 QGridLayout
调整为我想要的方式,以及 [=12] =] 工作仍然让我感到困惑,我不完全理解它是如何工作的。
这是我为此 windows 布局编写的代码:
self.pathRoot = QDir.rootPath()
self.labelFileName = QLabel(self)
self.labelFileName.setText("Search:")
self.labelFileName.resize(100, 30)
self.txtSearch = QLineEdit(self)
self.txtSearch.textChanged.connect(self.on_textChanged)
self.btnBack = QPushButton('Back', self)
self.btnBack.clicked.connect(self.back)
self.model = QFileSystemModel()
self.model.setRootPath(QDir.rootPath())
self.model.setFilter(QDir.NoDotAndDotDot | QDir.AllEntries | QDir.Dirs | QDir.Files)
self.proxy_model = QSortFilterProxyModel(recursiveFilteringEnabled = True, filterRole = QFileSystemModel.FileNameRole)
self.proxy_model.setSourceModel(self.model)
self.model.setReadOnly(False)
self.model.setNameFilterDisables(False)
self.indexRoot = self.model.index(self.model.rootPath())
self.treeView = QTreeView(self)
self.treeView.setModel(self.proxy_model)
self.adjust_root_index()
self.treeView.setRootIndex(self.indexRoot)
self.treeView.clicked.connect(self.on_treeView_clicked)
self.treeView.doubleClicked.connect(self.treeMedia_doubleClicked)
self.treeView.setAnimated(True)
self.treeView.setIndentation(20)
self.treeView.setSortingEnabled(True)
self.treeView.setDragEnabled(False)
self.treeView.setAcceptDrops(False)
self.treeView.setDropIndicatorShown(True)
self.treeView.setEditTriggers(QTreeView.NoEditTriggers)
self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
self.treeView.customContextMenuRequested.connect(self.showContextMenu)
for i in range(1, self.treeView.model().columnCount()):
self.treeView.header().hideSection(i)
self.gridLayout = QGridLayout()
self.gridLayout.addWidget(self.labelFileName, 0, 0)
self.gridLayout.addWidget(self.btnBack, 2, 0)
self.gridLayout.addWidget(self.txtSearch, 1, 0)
self.gridLayout.addWidget(self.treeView, 3, 0)
self.gridLayout1 = QGridLayout()
self.progressbar = QProgressBar(self)
self.lblProgress = QLabel('0/0', self)
self.btnClearBatches = QPushButton('Clear Batches', self)
self.btnClearBatches.clicked.connect(self.clear_batches)
self.layout = QHBoxLayout(self)
self.layout.addLayout(self.gridLayout)
self.layout.addLayout(self.gridLayout1)
self.scroll = QScrollArea(self)
self.gridLayout1.addWidget(self.scroll, 0, 0)
self.gridLayout1.addWidget(self.lblProgress, 1, 0)
self.gridLayout1.addWidget(self.progressbar, 2, 0)
self.gridLayout1.addWidget(self.btnClearBatches, 3, 0)
self.scroll.move(7, 80)
self.scroll.setWidgetResizable(True)
self.content = QWidget()
self.scroll.setWidget(self.content)
self.lay = QGridLayout(self.content)
没有必要仅仅使用QGridLayout 来尝试构建所有内容,例如设置左右块的拉伸因子您可以使用QWidget 作为容器。由于您不提供 MRE,我从头开始设计。
import sys
from PyQt5 import QtWidgets
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
label = QtWidgets.QLabel("Label")
button = QtWidgets.QPushButton("Button")
lineedit = QtWidgets.QLineEdit()
left_gridlayout = QtWidgets.QGridLayout()
right_gridlayout = QtWidgets.QGridLayout()
left_widget = QtWidgets.QWidget()
left_widget.setContentsMargins(0, 0, 0, 0)
vbox = QtWidgets.QVBoxLayout(left_widget)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.addWidget(label)
button_and_lineedit_container = QtWidgets.QWidget()
hlay_2 = QtWidgets.QHBoxLayout(button_and_lineedit_container)
hlay_2.setContentsMargins(0, 0, 0, 0)
hlay_2.addWidget(button)
hlay_2.addWidget(lineedit, stretch=1)
vbox.addWidget(button_and_lineedit_container)
bottom_container = QtWidgets.QWidget()
bottom_container.setContentsMargins(0, 0, 0, 0)
bottom_container.setLayout(left_gridlayout)
vbox.addWidget(bottom_container, stretch=1)
right_widget = QtWidgets.QWidget()
right_widget.setLayout(right_gridlayout)
hlay = QtWidgets.QHBoxLayout(w)
hlay.addWidget(left_widget, stretch=1)
hlay.addWidget(right_widget, stretch=3)
# debug
bottom_container.setStyleSheet("background-color:salmon;")
right_widget.setStyleSheet("background-color:gray;")
w.resize(640, 480)
w.show()
sys.exit(app.exec())
这是我当前的 window 布局,我想将它分成 1/4,而不是 1/2 可以这么说。
这就是我希望我的 window 看起来的样子,但我似乎找不到一个好的方法来将 QGridLayout
调整为我想要的方式,以及 [=12] =] 工作仍然让我感到困惑,我不完全理解它是如何工作的。
这是我为此 windows 布局编写的代码:
self.pathRoot = QDir.rootPath()
self.labelFileName = QLabel(self)
self.labelFileName.setText("Search:")
self.labelFileName.resize(100, 30)
self.txtSearch = QLineEdit(self)
self.txtSearch.textChanged.connect(self.on_textChanged)
self.btnBack = QPushButton('Back', self)
self.btnBack.clicked.connect(self.back)
self.model = QFileSystemModel()
self.model.setRootPath(QDir.rootPath())
self.model.setFilter(QDir.NoDotAndDotDot | QDir.AllEntries | QDir.Dirs | QDir.Files)
self.proxy_model = QSortFilterProxyModel(recursiveFilteringEnabled = True, filterRole = QFileSystemModel.FileNameRole)
self.proxy_model.setSourceModel(self.model)
self.model.setReadOnly(False)
self.model.setNameFilterDisables(False)
self.indexRoot = self.model.index(self.model.rootPath())
self.treeView = QTreeView(self)
self.treeView.setModel(self.proxy_model)
self.adjust_root_index()
self.treeView.setRootIndex(self.indexRoot)
self.treeView.clicked.connect(self.on_treeView_clicked)
self.treeView.doubleClicked.connect(self.treeMedia_doubleClicked)
self.treeView.setAnimated(True)
self.treeView.setIndentation(20)
self.treeView.setSortingEnabled(True)
self.treeView.setDragEnabled(False)
self.treeView.setAcceptDrops(False)
self.treeView.setDropIndicatorShown(True)
self.treeView.setEditTriggers(QTreeView.NoEditTriggers)
self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
self.treeView.customContextMenuRequested.connect(self.showContextMenu)
for i in range(1, self.treeView.model().columnCount()):
self.treeView.header().hideSection(i)
self.gridLayout = QGridLayout()
self.gridLayout.addWidget(self.labelFileName, 0, 0)
self.gridLayout.addWidget(self.btnBack, 2, 0)
self.gridLayout.addWidget(self.txtSearch, 1, 0)
self.gridLayout.addWidget(self.treeView, 3, 0)
self.gridLayout1 = QGridLayout()
self.progressbar = QProgressBar(self)
self.lblProgress = QLabel('0/0', self)
self.btnClearBatches = QPushButton('Clear Batches', self)
self.btnClearBatches.clicked.connect(self.clear_batches)
self.layout = QHBoxLayout(self)
self.layout.addLayout(self.gridLayout)
self.layout.addLayout(self.gridLayout1)
self.scroll = QScrollArea(self)
self.gridLayout1.addWidget(self.scroll, 0, 0)
self.gridLayout1.addWidget(self.lblProgress, 1, 0)
self.gridLayout1.addWidget(self.progressbar, 2, 0)
self.gridLayout1.addWidget(self.btnClearBatches, 3, 0)
self.scroll.move(7, 80)
self.scroll.setWidgetResizable(True)
self.content = QWidget()
self.scroll.setWidget(self.content)
self.lay = QGridLayout(self.content)
没有必要仅仅使用QGridLayout 来尝试构建所有内容,例如设置左右块的拉伸因子您可以使用QWidget 作为容器。由于您不提供 MRE,我从头开始设计。
import sys
from PyQt5 import QtWidgets
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
label = QtWidgets.QLabel("Label")
button = QtWidgets.QPushButton("Button")
lineedit = QtWidgets.QLineEdit()
left_gridlayout = QtWidgets.QGridLayout()
right_gridlayout = QtWidgets.QGridLayout()
left_widget = QtWidgets.QWidget()
left_widget.setContentsMargins(0, 0, 0, 0)
vbox = QtWidgets.QVBoxLayout(left_widget)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.addWidget(label)
button_and_lineedit_container = QtWidgets.QWidget()
hlay_2 = QtWidgets.QHBoxLayout(button_and_lineedit_container)
hlay_2.setContentsMargins(0, 0, 0, 0)
hlay_2.addWidget(button)
hlay_2.addWidget(lineedit, stretch=1)
vbox.addWidget(button_and_lineedit_container)
bottom_container = QtWidgets.QWidget()
bottom_container.setContentsMargins(0, 0, 0, 0)
bottom_container.setLayout(left_gridlayout)
vbox.addWidget(bottom_container, stretch=1)
right_widget = QtWidgets.QWidget()
right_widget.setLayout(right_gridlayout)
hlay = QtWidgets.QHBoxLayout(w)
hlay.addWidget(left_widget, stretch=1)
hlay.addWidget(right_widget, stretch=3)
# debug
bottom_container.setStyleSheet("background-color:salmon;")
right_widget.setStyleSheet("background-color:gray;")
w.resize(640, 480)
w.show()
sys.exit(app.exec())