gpt4 book ai didi

python - 使用 QFileSystemModel 时索引 PyQt5 组合框

转载 作者:行者123 更新时间:2023-12-01 00:19:27 27 4
gpt4 key购买 nike

我需要从文件目录列表中选择出现在组合框中的默认文件。使用普通的组合框,很容易使用 .findText 找到所需值的索引,但这似乎不适用于 QFileSystemModel 组合框,可能是因为选项列表在目录列表可以填充之前不会填充。获得资源。

这是我尝试过的:

import sys
import collections
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QComboBox
from PyQt5.QtCore import QSize, QRect

class ComboWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(640, 140))
self.setWindowTitle("Combobox example")

centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)

# Create combobox and add items.
self.fsm = QtWidgets.QFileSystemModel()
self.fsm.setNameFilters(["*.txt"])
self.configComboBox = QtWidgets.QComboBox(self)
self.configComboBox.setGeometry(QRect(40, 40, 491, 31))
self.configComboBox.setObjectName(("comboBox"))
self.configComboBox.setModel(self.fsm)
self.fsm.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
# "text_files" is a subdir of current dir with files
# example1.txt, example2.txt, example3.txt
self.configComboBox.setRootModelIndex(self.fsm.setRootPath("text_files"))
# V V This section does not work V V
index = self.configComboBox.findText(MainConfig.settings["default_txt_file"])
self.configComboBox.setCurrentIndex(index)

class MainConfig:
settings = collections.OrderedDict()

@staticmethod
def createDefaultConfig(name):
MainConfig.settings["default_txt_file"] = "example3.txt"

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ComboWindow()
mainWin.show()
sys.exit( app.exec_() )

最佳答案

正如我在 this answer 中指出的那样,QFileSystemModel在新线程中异步加载,因此您必须使用directoryLoaded信号来知道信息何时完成加载:

import collections
import os
import sys

from PyQt5.QtWidgets import (
QApplication,
QComboBox,
QFileSystemModel,
QMainWindow,
QWidget,
)
from PyQt5.QtCore import pyqtSlot, QDir, QRect, QSize


class ComboWindow(QMainWindow):
def __init__(self, parent=None):
super(ComboWindow, self).__init__(parent=None)

self.setMinimumSize(QSize(640, 140))
self.setWindowTitle("Combobox example")

centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)

# Create combobox and add items.
self.fsm = QFileSystemModel()
self.fsm.setNameFilters(["*.txt"])
self.configComboBox = QComboBox(self)
self.configComboBox.setGeometry(QRect(40, 40, 491, 31))
self.configComboBox.setObjectName(("comboBox"))
self.configComboBox.setModel(self.fsm)
self.fsm.setFilter(QDir.NoDotAndDotDot | QDir.Files)
self.fsm.directoryLoaded.connect(self.on_directoryLoaded)
current_dir = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.join(current_dir, "text_files")
self.configComboBox.setRootModelIndex(self.fsm.setRootPath(dir_path))

@pyqtSlot(str)
def on_directoryLoaded(self, path):
index = self.configComboBox.findText(MainConfig.settings["default_txt_file"])
self.configComboBox.setCurrentIndex(index)


class MainConfig:
settings = collections.OrderedDict()

@staticmethod
def createDefaultConfig(name):
MainConfig.settings["default_txt_file"] = name


if __name__ == "__main__":
MainConfig.createDefaultConfig("example3.txt")
app = QApplication(sys.argv)
mainWin = ComboWindow()
mainWin.show()
sys.exit(app.exec_())

关于python - 使用 QFileSystemModel 时索引 PyQt5 组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com