qt - 更改数据库时,pyqt QTableView 不填充

发布于 2024-08-09 20:40:23 字数 3465 浏览 1 评论 0原文

我试图允许我的用户选择要打开的数据库。每个数据库都将具有相同的架构。由于某种原因,我打开数据库后无法填充 QTableView。

我正在解释示例代码,但这应该让您了解我正在尝试做什么。

有效:

class aMainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.db = QSqlDatabase.addDatabase("QSQLITE")

        self.db.setDatabaseName('testdb.db')
        self.db.open()   

        # Set up the user interface from Designer.
        self.setupUi(self)

        #self.db.setDatabaseName('testdb.db')

        self.model = QSqlTableModel(self)
        self.model.setTable("records")

        self.model.setSort(FILEORDER, Qt.AscendingOrder)

        self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
        self.model.setHeaderData(FILEORDER, Qt.Horizontal, QVariant("File Order"))
        self.model.setHeaderData(RECORDTYPE, Qt.Horizontal, QVariant("Type"))
        self.model.setHeaderData(NAME, Qt.Horizontal, QVariant("Name"))
        self.model.setHeaderData(PRESORTNAME, Qt.Horizontal, QVariant("Presort Name"))
        self.model.setHeaderData(RECORD, Qt.Horizontal, QVariant("Record"))
        self.model.select()

        self.tableView.setModel(self.model)
        #self.view.setSelectionMode(QTableView.SingleSelection)
        #self.view.setSelectionBehavior(QTableView.SelectRows)
        self.tableView.setColumnHidden(ID, True)
        self.tableView.setColumnHidden(PRESORTNAME, True)
        self.tableView.setColumnHidden(RECORD, True)

无效:

class aMainWindow(QMainWindow, Ui_MainWindow):       
    def __init__(self):
        QMainWindow.__init__(self)

        # Set up the user interface from Designer.
        self.setupUi(self)

        #self.db.setDatabaseName('testdb.db')

        self.model = QSqlTableModel(self)
        self.model.setTable("records")

        self.model.setSort(FILEORDER, Qt.AscendingOrder)

        self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
        self.model.setHeaderData(FILEORDER, Qt.Horizontal, QVariant("File Order"))
        self.model.setHeaderData(RECORDTYPE, Qt.Horizontal, QVariant("Type"))
        self.model.setHeaderData(NAME, Qt.Horizontal, QVariant("Name"))
        self.model.setHeaderData(PRESORTNAME, Qt.Horizontal, QVariant("Presort Name"))
        self.model.setHeaderData(RECORD, Qt.Horizontal, QVariant("Record"))
        self.model.select()

        self.tableView.setModel(self.model)
        #self.view.setSelectionMode(QTableView.SingleSelection)
        #self.view.setSelectionBehavior(QTableView.SelectRows)
        self.tableView.setColumnHidden(ID, True)
        self.tableView.setColumnHidden(PRESORTNAME, True)
        self.tableView.setColumnHidden(RECORD, True) 


    #slot of the open db action
    def on_actionOpen_DB_triggered(self, checked=None):
        if checked is None: return
        filename = QFileDialog.getOpenFileName(self, 'open a database', 
                        '/home/',
                        "Databases (*.db)", #All Files (*.*)
                        "Databases (*.db)")
        if not filename:
            pass

        self.db = QSqlDatabase.addDatabase("QSQLITE")


        if self.db.isOpen():
            sys.stdout.write('db still open?')            

        self.db.setDatabaseName(filename)
        self.dbname = filename
        self.db.open()
        self.model.select()
        #self.tableView.update()
        if self.db.isOpen():
            sys.stdout.write('db opened')

I'm trying to allow my users to pick which database to open. Each database will have the same schema. For some reason though I can't get my QTableView to populate after I open the database.

I'm paraphrasing the example code but this should give you an idea of what I'm trying to do.

works:

class aMainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.db = QSqlDatabase.addDatabase("QSQLITE")

        self.db.setDatabaseName('testdb.db')
        self.db.open()   

        # Set up the user interface from Designer.
        self.setupUi(self)

        #self.db.setDatabaseName('testdb.db')

        self.model = QSqlTableModel(self)
        self.model.setTable("records")

        self.model.setSort(FILEORDER, Qt.AscendingOrder)

        self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
        self.model.setHeaderData(FILEORDER, Qt.Horizontal, QVariant("File Order"))
        self.model.setHeaderData(RECORDTYPE, Qt.Horizontal, QVariant("Type"))
        self.model.setHeaderData(NAME, Qt.Horizontal, QVariant("Name"))
        self.model.setHeaderData(PRESORTNAME, Qt.Horizontal, QVariant("Presort Name"))
        self.model.setHeaderData(RECORD, Qt.Horizontal, QVariant("Record"))
        self.model.select()

        self.tableView.setModel(self.model)
        #self.view.setSelectionMode(QTableView.SingleSelection)
        #self.view.setSelectionBehavior(QTableView.SelectRows)
        self.tableView.setColumnHidden(ID, True)
        self.tableView.setColumnHidden(PRESORTNAME, True)
        self.tableView.setColumnHidden(RECORD, True)

doesn't work:

class aMainWindow(QMainWindow, Ui_MainWindow):       
    def __init__(self):
        QMainWindow.__init__(self)

        # Set up the user interface from Designer.
        self.setupUi(self)

        #self.db.setDatabaseName('testdb.db')

        self.model = QSqlTableModel(self)
        self.model.setTable("records")

        self.model.setSort(FILEORDER, Qt.AscendingOrder)

        self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
        self.model.setHeaderData(FILEORDER, Qt.Horizontal, QVariant("File Order"))
        self.model.setHeaderData(RECORDTYPE, Qt.Horizontal, QVariant("Type"))
        self.model.setHeaderData(NAME, Qt.Horizontal, QVariant("Name"))
        self.model.setHeaderData(PRESORTNAME, Qt.Horizontal, QVariant("Presort Name"))
        self.model.setHeaderData(RECORD, Qt.Horizontal, QVariant("Record"))
        self.model.select()

        self.tableView.setModel(self.model)
        #self.view.setSelectionMode(QTableView.SingleSelection)
        #self.view.setSelectionBehavior(QTableView.SelectRows)
        self.tableView.setColumnHidden(ID, True)
        self.tableView.setColumnHidden(PRESORTNAME, True)
        self.tableView.setColumnHidden(RECORD, True) 


    #slot of the open db action
    def on_actionOpen_DB_triggered(self, checked=None):
        if checked is None: return
        filename = QFileDialog.getOpenFileName(self, 'open a database', 
                        '/home/',
                        "Databases (*.db)", #All Files (*.*)
                        "Databases (*.db)")
        if not filename:
            pass

        self.db = QSqlDatabase.addDatabase("QSQLITE")


        if self.db.isOpen():
            sys.stdout.write('db still open?')            

        self.db.setDatabaseName(filename)
        self.dbname = filename
        self.db.open()
        self.model.select()
        #self.tableView.update()
        if self.db.isOpen():
            sys.stdout.write('db opened')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2024-08-16 20:40:23

我今天不记得在哪里找到它,但当我研究其他东西时,我发现一些论坛帖子说必须在制作模型之前建立连接。我怀疑模型构造中一定有一些代码接触到数据库。我更改了 on_actionOpen_DB_triggered 以在建立连接后创建模型,并且效果很好。

I can't remember today exactly where I found it but as I was researching something else I found some forum posting that said the connection must be made before making the model. I suspect there must be some code in the model construct that's touching the db. I changed my on_actionOpen_DB_triggered to create the model after making the connection and it works just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文