初始选择后PyQt4组合框未更新

2024-05-04 22:32:26 发布

您现在位置:Python中文网/ 问答频道 /正文

当从sqlite中选择表名以显示在Combobox tabSelection中时,Combobox dbSelection不会更新为在代码中应用。在

单击按钮后,加载目录对话框窗口也需要相当长的时间。在

我还想确保数据库中的所有表都列在tabSelection组合框中。在

代码如下所示,并与Qt设计器文件相关联:

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_()

Tags: nameinimportselfforsqlitefiletablelist
1条回答
网友
1楼 · 发布于 2024-05-04 22:32:26

你考虑过用QTableView代替QTableList吗?
Qt使用MVC,这种方法比手工操作快得多。在

在这种情况下,这非常简单。
创建模型:

my_model =  QStringListModel()

然后可以将数据保存到my_model

^{pr2}$

一旦有了QTableView,就可以使用它的方法setModel()来提供模型。在

my_table_view.setModel(my_model)

相关问题 更多 >