为QLineEdit切换QCompleter时出现分段错误

时间:2013-11-03 09:20:09

标签: python pyqt4 textchanged qcompleter

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

仅为示例我简单地将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 有没有更好的方法可以同时使用两个完成者?

0 个答案:

没有答案