0

I am using the QSqlTableModel and the QTableView. I want to be able to add and drop columns via the menubar in the GUI. When I trigger the SQL query, the db is altered as expected but the view won't update. I have to close and rerun the application to see the altered version of the db.

Here are the methods that are called when clicking on the respective entries in the menu:

 def add_db_column(self):
    query = QSqlQuery()
    query.exec("ALTER TABLE Names ADD NewColumn varchar(255)")
    query.finish()

def drop_column(self):
    query = QSqlQuery()
    query.exec("ALTER TABLE Names DROP NewColumn")
    query.finish

I tried to include self.update(), self.model.layoutChanged.emit() and self.select() after the query in those methods, but the view just won't update.

What am I doing wrong here?

Ben
  • 1
  • 2
  • There is no QSqlTableView, only QSqlTableModel. Call again `model.setTable()` and `model.select()` after changing the model. Note that you missed the parentheses in the second `query.finish`, but consider that it's normally unnecessary to call that, especially for a local query that will be destroyed when the function returns. – musicamante May 22 '22 at 13:57
  • Thanks, that worked! The "Sql" in "QTableView" slipped in there somehow in the title. I edited the title of the question to avoid future misunderstandings... – Ben May 22 '22 at 18:06

0 Answers0