Qt calling method in another class

Asked

Viewed 354 times

5

I’m calling a method another class after entering data. But it 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:

for 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 the tableView_vendas after the insertion of the sale.

  • You are opening more than one instance of the database connection in your application?

  • I have in the Mainmain builder base_dados = new Conexao(NOME_BASE_DADOS);

  • The connection is once in MainPrincipal::MainPrincipal(){base_dados = new Conexao(NOME_BASE_DADOS);} . You thought the problem was opening up new MainPrincipal he’s running around all class again.

  • So that’s it. This connection needs to be opened and exist only once in the application, right?

  • Yes. Look at the top of the code, I added the constructor that has the database link.

  • Try using thread lock

Show 1 more comment

1 answer

1


I’m not so sure absolute that this is the problem, as you have not put the code(s) (s) running the method inserte_venda. Anyway, the problem seems to be right there.

The method void DialogVenda::inserte_venda() contains code to create a new instance of the type window MainPrincipal, inherited from QMainWindow, every time it is called (what it does by running the line MainPrincipal *pt= new MainPrincipal(this);). There is not necessarily anything wrong with this, but note that the constructor of this main window class creates the connection to the database and opens it, as you yourself notice in your comments. Therefore, the connection is created and opened every call of inserte_venda, indirectly by the manufacturer of MainPrincipal.

Your code does not contain a destroyer for the class MainPrincipal, then the created and open connection is not being closed. Anyway, you probably also have a potential memory leak in the sense that every instance of MainPrincipal created at each call of inserte_venda is being tied to the instance of DialogVenda (because of the this that you pass in the builder of MainPrincipal). If DialogVenda not leaving the scope, the instance created for MainPrincipal also will not exit automatically (automatic deletion of Qt works this way depending on the parent object being deleted). So even if you had a destroyer, maybe it didn’t work as expected.

A solution would be for you not to instantiate the class MainPrincipal with the operator New, but rather do MainPrincipal pt; and then pt.tableView_listaVendas();. Thus, pt will be instantiated in the pile (stack) instead of heap, and will be destroyed when the scope is closed (that is, when the method inserte_venda break up).

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

Browser other questions tagged

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