Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call db.close() on Button_Click (QT/C++)

how can i close a database connction from Button_onClick funktion?

    Artikelverwaltung::Artikelverwaltung(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Artikelverwaltung)
    {
      ...
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    ...
    }
    
    void Artikelverwaltung::on_pushButton_clicked()
    {
        db.close(); // <---- This is not working of cause. how can i do this?
}

Best regards

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is not working because db is a local variable in Artikelverwaltung::Artikelverwaltung you need to make it a class attribute.

class Artikelverwaltung
{
  private:
    QSqlDatabase m_db;
};




Artikelverwaltung::Artikelverwaltung(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Artikelverwaltung)
{
  ...

    m_db = QSqlDatabase::addDatabase("QODBC");
...
}

void Artikelverwaltung::on_pushButton_clicked()
{
    m_db.close();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading