Rob
Rob

Reputation: 169

how can I make a connection to a database in the main function and then reference it from other functions

I have recently begun delving into the world that is database programming with C++ & Qt, and I've got a question. I know the answer is probably very basic but I can't seem to figure it out.

How can I make a connection to a database in the main function and then reference it from other functions?

Basically when the program first launches I create a instance of the main window, but before showing it, I connect to a database for later queries. I then show the window. Great but no when I try to make the window populate with data I can't because the connection name is outside of the scope of the function.

How could I make that global, I thought it would be by default because I have constructed the database in the main function witch every other class or function should inherit from(or at least I thought).

I'm still learning so I'm sure it is just a small misconception I have on how inheritance works.

Here is my main function

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LOCALHOST\\TestServer");
db.setDatabaseName("TestConnection");

w.show();


return a.exec();
}

And later on in the main window I create a dock which I can populate with data pulled from a database. Here is were I need to preform the Query in the mainwindow::Createdock function

void MainWindow::createDockWindows()
{
QDockWidget *rightDock = new QDockWidget(tr("Tasks"),this);
rightDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

todoGroupBox = new QGroupBox(rightDock);
todoGroupBox->setTitle(tr("To-Do List"));

QSqlDatabase(db.open());
if (db.open())
{
    QMessageBox::information(this,"Connected","Connection to the Database was Established\n"
                             "\nStatus: Connected");
}
else
{
    QMessageBox::information(this,"Not Connected","Connection to the Database could not be Established\n"
                             "\nStatus: Not Connected");
}

todoList = new QListWidget(todoGroupBox);
todoList->addItems(QStringList()
        << "Install Outlook on Jessies Computer"
        << "Purchase 2 Licenses of Adobe Suite"
        << "Contact UPS to put in a ticket to their IT dept.");

addToDoLabel = new QLabel;
addToDoLabel->setText(tr("Sample Text"));
addToDoButton = new QToolButton;
addToDoButton->setIcon(QIcon(":/images/gedit-icon.png"));
addToDoButton->setToolTip(tr("Create a new task"));
addToDoButton->setShortcut(tr("Ctrl+Shift+t"));

QHBoxLayout *todoButtonLayout = new QHBoxLayout();
todoButtonLayout->addWidget(addToDoLabel,0,Qt::AlignRight | Qt::AlignVCenter);
todoButtonLayout->addWidget(addToDoButton,0,Qt::AlignRight | Qt::AlignTop);

QVBoxLayout *todoLayout = new QVBoxLayout();
todoLayout->addWidget(todoList,0,Qt::AlignCenter);
todoLayout->addLayout(todoButtonLayout,0);

todoGroupBox->setLayout(todoLayout);
todoGroupBox->setMaximumSize(todoGroupBox->sizeHint());

}

Upvotes: 1

Views: 2099

Answers (1)

Karlson
Karlson

Reputation: 3048

The simplest way of doing this in QT would be to name the database connection:

When you create add a database using QT you can call addDatabase() specifying a database connection name. When you do that you can in any class use database() and provide a database handle to the one you have created in main() or any other place in the code since both methods are static.

Example

db = QSqlDatabase::addDatabase("QODBC", "MyDB");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
if (db.open()) {
    // success!
}

Then anywhere in the code you can do:

QSqlDatabase localdb = QSqlDatabase::database("MyDB");

which will give you the handle to the one you have opened above.

Upvotes: 2

Related Questions