Update a TableView

1

How can I update a TableView after closing a Dialog?

Example: main.ccp abre conexão com banco de dados

principal::principal(){
//..etc..//
model->setTable("nomeTabela");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setHeaderData(0, Qt::Horizontal, tr("Lista da tabela"));
model->select();
}

Dialog.cpp Apenas query de insert na tabela nomeTabela

void Dialog::on_cadastroDados_clicked()
{
   QSqlQuery qry;
   qry.prepare("INSERT INTO nomeTabela/..
   qry.addBindValue/...
   qry.exec/..
   //true
   QDialog::Closet();            
}
    
asked by anonymous 19.03.2014 / 14:08

1 answer

0

You can run setModel() back to TableView , shortly after closing Dialog :

// ...
model->select();
ui->tableView1->setModel(model);

Assuming the name of your TableView is tableView1 .

You call this after the user closes the dialog, for example:

DialogoExemplo dlg(this);

// Mostra o dialogo e espera o usuário retornar ou cancelar a janela.
if( dlg.exec() == QDialog::Accepted )
{
    // O usuário fechou o dialogo corretamente (não cancelou).
    // Pare o caso dlg ser fechado usando QDialog::accept().
    model->select();
    ui->tableView1->setModel(model);
    return;
}
    
19.03.2014 / 14:22