简体   繁体   中英

PyQt4 Combobox Not Updating After Initial Selection

Combobox dbSelection does not update to apply in code when selecting table names from sqlite to display in combobox tabSelection.

It also takes a significant number of seconds to load the directory dialog window once the button is clicked.

I would also like to ensure that all tables within a database are listed in the tabSelection combobox.

The code is as follows and is associated with a Qt Designer file:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import qcDbWidget2
import os
import sqlite3

class MainDialog(QWidget, qcDbWidget2.Ui_qcQueryWidget):
    def __init__(self, parent=None):
        super(MainDialog, self).__init__(parent)
        self.setupUi(self)
        self.connect(self.dbDirSelect, SIGNAL("clicked()"), self.getDirName)

    def getDirName(self):
        existDir = QFileDialog.getExistingDirectory(self)
        dbDir = str(existDir)
        self.dbDirDisplay.setText(dbDir)

        dbFileList = []
        for root, dirs, files in os.walk(dbDir):
            for file in files:
                if file.endswith('.db'):
                    dbFileList.append(file)

        self.dbSelection.addItems(dbFileList)

        tableList = []
        self.dbSelection.update()

        dbName = str(self.dbSelection.currentText())
        dbPath = str(dbDir + '\\' + dbName)
        conn = sqlite3.connect(dbPath)
        c = conn.cursor()

        res = c.execute("SELECT name FROM sqlite_master WHERE type='table';")
        self.tabSelection.clear()
        for name in res:
            tableList.append(name[0])
            print(name[0])
        for name in tableList:
           self.tabSelection.addItems(tableList)

app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()

Have you considered to use QTableView instead of QTableList ?
Qt uses MVC which methods are way faster than doing it by hand.

In this case, it's pretty simple.
You create a model:

my_model =  QStringListModel()

You can then save data to my_model :

my_list = ["toto", "tutu", "tata"]
my_model.setStringList(my_list)

Once you have a QTableView , you use it's method setModel() to provide the model.

my_table_view.setModel(my_model)

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