简体   繁体   中英

Python PyQt QLineEdit to Search bar

am trying to build translator and put a search bar in it .with QLineEdit and what i want is auto complet the word . .. i tried this . but this code is not working , i'm talking about SearchBar function . but the rest of the code is working with the rest program just fine . but the SearchBar function not working . and not completing what i type in 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)

The functions setModel() and setCompleter() are void, return no value.

You can write like this:

    AraKey = ['a','ab','abc']
    ModAra = QtGui.QStringListModel()
    ModAra.setStringList(AraKey)
    ComAra = QtGui.QCompleter()
    ComAra.setModel(ModAra)
    self.LineEditAra.setCompleter(ComAra)

I have tried this, and it really works.

i found it .. it must pass a list to SearchBar function not a dict .. so this works ..

    # 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM