在运行时更新qstackedwidget内容



我正在PYQT5中创建一个GUI,该GUI应该从多个相关的SQLite表中提供多个视图。我已经实现了这些视图将通过qstackedwidget显示。

现在,其中一些视图用于概述目的,而另一些视图则是在概述中显示的数据子集上进行更多删除的视图。我想通过RightClick从概述中访问概述。

我提供了一个最小的例子,其中有以下汽车。(抱歉,这有点长,但是需要提供一个完整的工作示例。我尽可能地减少了它。(目标是向概述中选择的公司的汽车展示Detaillview。p>这已经提供了从RightClick上的DETAILLVIEW概述的访问权限,但是公司的信息未传递。因此,即使从"大众"访问detaillview时,self.mycompany也会更新,但car_widget却没有,因此detaillview显示了有关"本田"汽车的信息(默认值(。

有没有办法与合适的公司更新CAR_WIDGIT?还是我需要在运行时创建detaillview?(这甚至有效吗?

如何根据另一个视图中选择的内容在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()      

继承的目标之一是,班级实现了孩子可以做的常见任务,在您的情况下,您不观察到这些任务,例如创建模型必须在父亲中完成,因为所有的孩子都会做。

另一方面,使用QSQLTableModel的目标不是使用QSqlQuery,而是使用setTable()select()setFilter()等更友好的请求,但这只是浪费,因为您可以使用QSqlQueryModel

另一方面,我认为您正在假设MainGuiself.mycompanyDetailedViewself.company相同,而您正在创建deletionView对象时所经历的内容,事实是它们不是相同的,在创建对象时,仅复制了该时刻的值,因此,如果更改MainGuiself.company,它将不会更改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()  

相关内容

  • 没有找到相关文章

最新更新