0

我有来自两个来源的数据,一个来自 XML 文件保存路径,第二个来自QFileSystemModel我想要的工作,如果用户看不到路径,那么QLineEdit用户应该能够浏览路径!第一个字母显然是“/”或 windows 的单个字母。

仅对于示例,我已经简单地将 XMLData 从文件替换为 defaultList 的路径。

下面的代码工作了一段时间,但我得到了分段错误。

from PyQt4.Qt import Qt, QObject,QLineEdit
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT
from PyQt4 import QtGui, QtCore

import sys

class DirLineEdit(QLineEdit, QtCore.QObject):
    """docstring for DirLineEdit"""


def __init__(self):
    super(DirLineEdit, self).__init__()
    self.defaultList = ['~/Development/python/searchMethod', '~/Development/Nuke_python',
'~/Development/python/openexr', '~/Development/python/cpp2python',
 '~/Development/python/using_argparse', '~Development/python/listFilter']
    self.textChanged.connect(self.switchCompleter)
    self._pathsList()

def focusInEvent(self, event):
    QtGui.QLineEdit.focusInEvent(self, event)
    self.completer().complete()

def switchCompleter(self):
    if len(self.text()) >= 1:
        self.__dirCompleter()
    if len(self.text()) == 0:
        self.__pathsList()

def __dirCompleter(self):
    dirModel = QtGui.QFileSystemModel() 
    dirModel.setRootPath(QtCore.QDir.currentPath()) 
    dirModel.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files) 
    dirModel.setNameFilterDisables(0) 
    completer = QtGui.QCompleter(dirModel, self)    
    completer.setModel(dirModel)
    completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) 
    self.setCompleter(completer)

def _pathsList(self):
    completerList = QtCore.QStringList()
    for i in self.defaultList:
        completerList.append(QtCore.QString(i))
    lineEditCompleter = QtGui.QCompleter(completerList)
    lineEditCompleter.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
    self.setCompleter(lineEditCompleter)



app = QtGui.QApplication(sys.argv)
smObj = DirLineEdit()
smObj.show()    
app.exec_()

上面的代码给出了分段错误:11 有没有更好的方法可以同时使用两个完成器?

4

0 回答 0