tags:

views:

567

answers:

1

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')
+1  A: 

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.

AaronS