Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7704029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:35:37+00:00 2026-05-31T23:35:37+00:00

I have recently begun delving into the world that is database programming with C++

  • 0

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());

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:35:38+00:00Added an answer on May 31, 2026 at 11:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just recently begun to use markdown and have noticed that any markdown within
I usually script/program using python but have recently begun programming with JavaScript and have
I have recently begun to think about optimisation, now I know there are many
I have recently begun using coderush. One thing that annoys me is that the
I have only recently begun programming in Python (with previous Ruby experience). I am
I have recently begun working on a pre-existing PHP application, and am having a
I've recently begun learning C# but have encountered an annoying problem. Every variable I
I have recently run into a particularly sticky issue regarding committing the result of
I have recently begun experiencing several odd issues with the Android SDK and I
I have recently begun using Scala. I've written a DSL in it which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.