Update a Tableview

Asked

Viewed 69 times

1

How can I update a Tableview after closing a Dialog?

Example: principal.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();            
}

1 answer

0


You can run a setModel() back to the TableView, right after closing the Dialog:

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

Assuming the name of your TableView be it 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;
}
  • You speak right after closing Dialog. Where would be ?

  • @user628298 I added an example to answer.

  • Returned a error: cannot call member function 'bool QWidget::close()' without object
 if(dcl->exec() == QDialog::close())
 ^

  • Use the this->accept() (in the clase of the dialog) to close. And use QDialog::Accepted in the if. If you treat only the case of closing, you will accept when the user opens the dialog and close without making any changes.

  • It worked, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.