我正在尝试构建翻译器并在其中放入一个搜索栏。使用QLineEdit 我想要的是自动完成这个词。 .. 我试过这个。但这段代码不起作用,我在谈论SearchBar功能。但其余代码正在使用其余程序就好了。但SearchBar功能无法正常工作。并没有完成我在LineEdit中输入的内容
from PyQt4 import QtGui,QtCore
import sys
from MainWin import Ui_MainWindow
import sqlite3
conn = sqlite3.connect('DictDB.db')
cors = conn.cursor()
class MainApp(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self):
super(MainApp,self).__init__()
self.setupUi(self)
self.showMaximized()
cors.execute("SELECT * FROM DictContents")
for raw in cors.fetchall():
self.TextBrowserAra.append(raw[0])
self.TextBrowserGer.append(raw[1])
self.SearchBar(raw[0].strip(),raw[1].strip())
def SearchBar(self,keys,values):
mydict = {}
AraKey = mydict[0]=[keys]
GerKey = mydict[1]=[values]
Model = QtGui.QStringListModel()
ModAra = Model.setStringList(AraKey)
ModGer = Model.setStringList(GerKey)
completer = QtGui.QCompleter()
CompAra = completer.setModel(ModAra)
ComGer = completer.setModel(ModGer)
self.LineEditAra.setCompleter(CompAra)
self.LineEditGer.setCompleter(ComGer)
答案 0 :(得分:1)
函数setModel()
和setCompleter()
无效,不返回任何值。
您可以这样写:
AraKey = ['a','ab','abc']
ModAra = QtGui.QStringListModel()
ModAra.setStringList(AraKey)
ComAra = QtGui.QCompleter()
ComAra.setModel(ModAra)
self.LineEditAra.setCompleter(ComAra)
我试过这个,它确实有用。
答案 1 :(得分:0)
我发现它..它必须将列表传递给SearchBar函数而不是dict .. 所以这有效..
# first make an empty lists
self.AraList = []
self.GerList = []
for raw in cors.fetchall():
self.AraList.append(raw[0]) # put all data in one list
self.GerList.append(raw[1]) # " "
self.SearchBar(self.AraList,self.GerList) # passing the lists to SearchBar Function
def SearchBar(self,keys,values):
print(keys) #make sure its returns one big list , Lets try the keys first
ModAra = QtGui.QStringListModel()
ModAra.setStringList(keys)
ComAra = QtGui.QCompleter()
ComAra.setModel(ModAra)
self.LineEditAra.setCompleter(ComAra)
# It worked just fine