0

My app opens the SQLite database connection when the MainWindow launches and closes it when the MainWindow is destroyed.

How do I access the database connection from other classes. I think I need to use a singleton format but haven't had any luck implementing it.

Here's what I have tried. It errors with.

C:\Users\cbennett\C++\CA_Letter_Generator\letter.cpp:9: error: expected primary-expression before '.' token
     QSqlQuery qLetter = MainWindow.getDB().selectAll("letters","ltID=" + QString::number(ID));
                                   ^

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loadButtons();
    this->adjustSize();
}

MainWindow::~MainWindow()
{
    db.close();
    delete ui;
}

void MainWindow::loadButtons(){
    db.setDatabaseName(dbPath);
    db.open();       
    //load buttons from db
}
cc_sqlite MainWindow::getDB(){
    return db;
}

letter.cpp

Letter::Letter(int ID){
    QSqlQuery qLetter = MainWindow.getDB().selectAll("letters","ltID=" + QString::number(ID));
    while (qLetter.next()) {
            int id = qLetter.value(0).toInt();
            QString name = qLetter.value(1).toString();
            QString body = qLetter.value(2).toString();
            QString sal = qLetter.value(3).toString();
            qDebug() << id << name << body << sal;
    }
}

letter.h

#ifndef LETTER_H
#define LETTER_H
#include "mainwindow.h"
#include "field.h"
#include "../CC_CPP/cc_sqlite.h"


class Letter
{
public:
    Letter();
    Letter(int ID);
private:
    vector<Field> vFields;

};

#endif // LETTER_H
Talon06
  • 1,756
  • 3
  • 27
  • 51
  • 1
    `MainWindow.getDB()` Isn't MainWindow a pointer? So you would need `MainWindow->getDB()` – drescherjm Feb 13 '17 at 17:51
  • 1
    How does `Letter` know about `MainWindow`? – drescherjm Feb 13 '17 at 17:52
  • 1
    From your edit it seems that `Letter` has no connection to `MainWindow`. – drescherjm Feb 13 '17 at 18:01
  • 1
    ***I think I need to use a singleton format*** You don't need a singleton. – drescherjm Feb 13 '17 at 18:02
  • How do I create a connection to the MainWindow? When I changed the . to -> gave me the following error – Talon06 Feb 13 '17 at 18:03
  • C:\Users\cbennett\C++\CA_Letter_Generator\letter.cpp:9: error: expected primary-expression before '->' token QSqlQuery qLetter = MainWindow->getDB().selectAll("letters","ltID=" + QString::number(ID)); ^ – Talon06 Feb 13 '17 at 18:03
  • 1
    One way is to pass a pointer to the `MainWindow` to `Letter`. A second way is to do something like `MainWindow* pMainWindow = QCoreApplication::instance()->findChild();` – drescherjm Feb 13 '17 at 18:05
  • 1
    Also you may just use `QSqlDatabase::database()` to get a connection to the database if you have just 1 database or use the connection name parameter if you have more. And then not have to use MainWindow at all. This example explains that: http://stackoverflow.com/a/17408989/487892 – drescherjm Feb 13 '17 at 18:11
  • This is exactly what I needed! I was able to modify the class I use for accessing the database and should now be able to access the database from any class I need! Thanks! – Talon06 Feb 13 '17 at 18:53

0 Answers0