在运行时更新 QStackedWidget 内容

Updating QStackedWidget content at Runtime

我正在 PyQT5 中创建一个 GUI,它应该提供来自多个相关 SQLite 表的数据的多个视图。我已经实现了这些视图以通过 QStackedWidget 显示。

现在,其中一些视图用于概览目的,其他视图用于更详细地查看概览中显示的数据子集。我想通过右键单击从概览中访问详细视图。

我在下面提供了一个最小的汽车示例。 (抱歉,它有点长,但需要提供一个完整的工作示例。我已经尽可能地减少了它。)目标是显示 DetailledView 以及在概览中选择的公司的汽车。

这已经提供了通过右键单击从概览访问 DetailledView 的权限,但公司信息并未传递。因此,即使从 'VW' 访问 DetailledView 时,self.mycompany 也会更新,但 car_widget 不会更新,因此 DetailledView 会显示有关 'Honda' 辆汽车的信息(默认)。

有没有办法通过合适的公司更新 car_widgit?还是我需要在运行时创建 DetailledView? (这真的有用吗?但无论如何,我宁愿不要这样做,因为它会使 Stack 的索引变得不可靠...)

如何根据在另一个视图中选择的内容更新 QStackedWidget 中一个视图的 QTableModel?

代码如下:

#!/usr/bin/python3

from PyQt5 import QtSql
from PyQt5.QtWidgets import (QMainWindow, QWidget, QApplication, QGridLayout, 
                             QStackedWidget, QTableView, QMenu)
from PyQt5.QtCore import Qt, QModelIndex
import sys

class MainGUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.mycompany = "Honda"
        self.create_connection()
        self.fill_tables()

        self.init_UI()

    def create_connection(self):
        self.db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
        self.db.setDatabaseName("test.db")
        if not self.db.open():
            print("Cannot establish a database connection to {}!".format(self.db_file))
            return False

    def fill_tables(self):
        self.db.transaction()
        q = QtSql.QSqlQuery()
        q.exec_("DROP TABLE IF EXISTS Manufacturers;")
        q.exec_("CREATE TABLE Manufacturers (CompanyId INT PRIMARY KEY, Name TEXT, Country TEXT);")
        q.exec_("INSERT INTO Manufacturers VALUES (1, 'VW', 'Germany');")
        q.exec_("INSERT INTO Manufacturers VALUES (2, 'Honda' , 'Japan');")

        q.exec_("DROP TABLE IF EXISTS Cars;")
        q.exec_("CREATE TABLE Cars (Model TEXT, Year INT, Company INT);")
        q.exec_("INSERT INTO Cars VALUES ('Civic', 2009, 'Honda');")
        q.exec_("INSERT INTO Cars VALUES ('Golf', 2013, 'VW');")
        q.exec_("INSERT INTO Cars VALUES ('Polo', 1999, 'VW');")
        self.db.commit()

    def init_UI(self):
        self.central_widget = QWidget()    
        self.setCentralWidget(self.central_widget)
        self.grid = QGridLayout()
        self.central_widget.setLayout(self.grid)
        self.setLayout(self.grid)
        self.make_stack()
        self.show()

    def make_stack(self):
        self.Stack = QStackedWidget(self)

        company_view = QWidget()
        layout = QGridLayout()
        company_view.setLayout(layout)
        self.company_widget = Overview()
        layout.addWidget(self.company_widget, 0, 0)
        self.company_widget.table.customContextMenuRequested.connect(self.open_menu)
        self.Stack.addWidget(company_view)

        car_view = QWidget()
        layout2 = QGridLayout()
        car_view.setLayout(layout2)
        car_widget = DetailedView(self.mycompany)
        layout2.addWidget(car_widget, 0, 0)
        self.Stack.addWidget(car_view)

        self.grid.addWidget(self.Stack, 0,1)

    def open_menu(self, pos):
        menu = QMenu()
        show_act = menu.addAction("Show cars")
        action = menu.exec_(self.company_widget.table.mapToGlobal(pos))
        if action == show_act:
            row = self.company_widget.table.indexAt(pos).row()
            myindex = self.company_widget.model.index(row, 1, QModelIndex())
            company = self.company_widget.model.data(myindex)
            self.mycompany = company
            self.Stack.setCurrentIndex(1)



class MyTable(QWidget):
    def __init__(self):
        super().__init__()
        self.create_connection()
        self.create_model()
        self.init_UI()

    def create_connection(self):
        self.db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
        self.db.setDatabaseName("test.db")
        if not self.db.open():
            print("Cannot establish a database connection to {}!".format(self.db_file))
            return False

    def create_model(self):
        self.model = None

    def init_UI(self):
        self.grid = QGridLayout()
        self.setLayout(self.grid)
        self.table = QTableView()
        self.table.setModel(self.model)
        self.table.setContextMenuPolicy(Qt.CustomContextMenu)

        self.grid.addWidget(self.table, 0, 0)

    def closeEvent(self, e):
        if (self.db.open()):
            self.db.close()

    def check_error(self, q):
        lasterr = q.lastError()
        if lasterr.isValid():
            print(lasterr.text())
            self.db.close()
            exit(1)


class Overview(MyTable):
    def __init__(self):
        super().__init__()

    def create_model(self):
        self.model = QtSql.QSqlTableModel()
        q = QtSql.QSqlQuery()
        query = "SELECT * from Manufacturers"
        q.exec_(query)
        self.model.setQuery(q)


class DetailedView(MyTable):
    def __init__(self, company):
        self.company = company
        super().__init__()

    def create_model(self):
        self.model = QtSql.QSqlTableModel()
        q = QtSql.QSqlQuery()
        query = "SELECT * from cars where company = '{}'".format(self.company)
        q.exec_(query)
        self.model.setQuery(q)



def main():
    app = QApplication(sys.argv)
    ex = MainGUI()
    ex.show()

    result = app.exec_()
    sys.exit(result)


if __name__ == '__main__':
    main()      

继承的 objectives 之一是 class 实现了 children 可以完成的常见任务,在你的情况下你没有观察到这些任务,例如,模型的创建必须在父亲中完成,因为所有 children 都可以。

另一方面,使用 QSqlTableModel 的目的不是使用 QSqlQuery,而是更友好的请求,如 setTable()select()setFilter(),但它是简直是浪费,因为你可以使用 QSqlQueryModel.

另一方面,我看到您假设 MainGuiself.mycompany 与您正在经历的 DetailedViewself.company 相同在创建 DetailView object 时,事实是它们并不相同,在创建 object 时只复制了那一刻的值,所以如果您更改 [= MainGui 的 18=] 它不会改变 DetailViewself.company

重组您的项目,您将获得以下内容:

#!/usr/bin/python3

from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
from PyQt5.QtWidgets import (QMainWindow, QWidget, QApplication, QVBoxLayout, 
                             QStackedWidget, QTableView, QMenu)
from PyQt5.QtCore import Qt, QModelIndex, pyqtSignal
import sys

DB_PATH = "test.db"

def create_connection():
    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName(DB_PATH)
    if not db.open():
        print("Cannot establish a database connection to {}!".format(DB_PATH))
        return False
    return True

def fill_tables():
    q = QSqlQuery()
    q.exec_("DROP TABLE IF EXISTS Manufacturers;")
    q.exec_("CREATE TABLE Manufacturers (CompanyId INT PRIMARY KEY, Name TEXT, Country TEXT);")
    q.exec_("INSERT INTO Manufacturers VALUES (1, 'VW', 'Germany');")
    q.exec_("INSERT INTO Manufacturers VALUES (2, 'Honda' , 'Japan');")

    q.exec_("DROP TABLE IF EXISTS Cars;")
    q.exec_("CREATE TABLE Cars (Model TEXT, Year INT, Company INT);")
    q.exec_("INSERT INTO Cars VALUES ('Civic', 2009, 'Honda');")
    q.exec_("INSERT INTO Cars VALUES ('Golf', 2013, 'VW');")
    q.exec_("INSERT INTO Cars VALUES ('Polo', 1999, 'VW');")

class MainGUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        self.central_widget = QWidget()    
        self.setCentralWidget(self.central_widget)
        self.lay = QVBoxLayout(self.central_widget)
        self.make_stack()

    def make_stack(self):
        self.stack = QStackedWidget()
        self.lay.addWidget(self.stack)

        self.company_widget = Overview()
        self.car_widget = DetailedView()
        self.stack.addWidget(self.company_widget)
        self.stack.addWidget(self.car_widget)
        self.company_widget.changedCompany.connect(self.changedCompany)
        self.car_widget.backSignal.connect(lambda: self.stack.setCurrentIndex(0))

    def changedCompany(self, company):
        self.car_widget.filter(company)
        self.stack.setCurrentIndex(1)

class SQLTable(QTableView):
    def __init__(self, table):
        super().__init__()
        self.init_UI()
        self.create_model(table)

    def create_model(self, table):
        self.model.setTable(table)
        self.model.select()

    def init_UI(self):
        self.model = QSqlTableModel()
        self.setModel(self.model)
        self.setContextMenuPolicy(Qt.CustomContextMenu)

class Overview(SQLTable):
    changedCompany = pyqtSignal(str)
    def __init__(self):
        SQLTable.__init__(self, "Manufacturers")
        self.customContextMenuRequested.connect(self.open_menu)

    def open_menu(self, pos):
        menu = QMenu()
        show_act = menu.addAction("Show cars")
        action = menu.exec_(self.mapToGlobal(pos))
        if action == show_act:
            row = self.indexAt(pos).row()
            ix = myindex = self.model.index(row, 1)
            company = self.model.data(ix)
            self.changedCompany.emit(company)

class DetailedView(SQLTable):
    backSignal = pyqtSignal()
    def __init__(self):
        SQLTable.__init__(self, "cars")
        self.customContextMenuRequested.connect(self.open_menu)

    def open_menu(self, pos):
        menu = QMenu()
        back_act = menu.addAction("Show Manufacturers")
        action = menu.exec_(self.mapToGlobal(pos))
        if action == back_act:
            self.backSignal.emit()

    def filter(self, company):
        self.model.setFilter("company='{}'".format(company))


def main():
    app = QApplication(sys.argv)
    if not create_connection():
        sys.exit(-1)
    fill_tables()
    ex = MainGUI()
    ex.show()

    result = app.exec_()
    sys.exit(result)

if __name__ == '__main__':
    main()