Qt calling the method in another class

5

I am calling a method from another class after inserting data. But this is returning an SQLite error:

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

Code:

example_insert.cpp

void DialogVenda::inserte_venda(){
     //QSqlQuery etc....
     if (qry.lastInsertId()>0){
            QMessageBox::information(this,"Cadastro de pedido", "Pedido cadastrado com sucesso.");
            MainPrincipal *pt= new MainPrincipal(this);
            pt->tableView_listaVendas();
     }
}

MainPrincipal.cpp

 MainPrincipal::MainPrincipal(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainPrincipal){

    ui->setupUi(this);
    base = new connection(NAME_BASE);
    if(!base->openBD()){
       QMessageBox::critical(this, "Erro", ERRO_BASE);
       return;
     }
 }   


void MainPrincipal::tableView_listaVendas(){
 model = new QSqlQueryModel;
    model->setQuery("SELEC * FROM empresa_estoqueSaida WHERE strftime('%Y-%m-%d', saida_dataCadastro)='"+dateTime.toString("yyyy-MM-dd")+"'");
    model->setHeaderData(0, Qt::Horizontal, tr("Cliente"));
    model->setHeaderData(1, Qt::Horizontal, tr("Endereço"));
    model->setHeaderData(2, Qt::Horizontal, tr("Valor"));
    model->setHeaderData(3, Qt::Horizontal, tr("Pagamento"));
    model->setHeaderData(6, Qt::Horizontal, tr("Data"));
    ui->tableView_vendas->setModel(model);
}

The goal is to update tableView_vendas after inserting the sale.

    
asked by anonymous 28.08.2015 / 01:06

1 answer

1

I'm not sure absolute that this is the problem because you have not put the code snippet (s) that execute the inserte_venda method. Anyway, the problem seems to be right there.

The void DialogVenda::inserte_venda() method contains code to create a new window instance of type MainPrincipal , inherited from QMainWindow , every time it is called (which it does by executing the MainPrincipal *pt= new MainPrincipal(this); line). There is not necessarily anything wrong with this, but beware of the fact that the constructor of this main window class creates the connection to the database and opens it, how do you even notice in your comments . So the connection is created and opened on every call of inserte_venda , indirectly by the MainPrincipal constructor.

Your code does not contain a destructor for the MainPrincipal class, so the connection created and opened is not being closed. In any case, you probably also have a potential memory leak, in the sense that every instance of MainPrincipal created at every inserte_venda call is being tied to the DialogVenda instance (because of this which you pass in the MainPrincipal constructor). If DialogVenda does not exit the scope, instances created for MainPrincipal will not automatically exit either (the automatic deletion of Qt works this way depending on whether the parent object is deleted). So even if you had a destructor, it might not work as expected.

One solution is that you do not instantiate the MainPrincipal class with the New operator, but instead do MainPrincipal pt; and then pt.tableView_listaVendas(); . This way, pt will be instantiated inserte_venda terminate).

But a better solution would be to separate the connection from any other class, especially if this class involves the graphical interface. In theory your application should check the connection at startup, open it, and keep it open during its runtime (or manage it dynamically but still separate). Try to build a data class that is widely accessible and unique. As for example using the singleton pattern .

    
16.03.2016 / 13:21