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

  • SEARCH
  • Home
  • 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 5850449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:08:32+00:00 2026-05-22T13:08:32+00:00

I have stackedwidget with multiple pages, at page one, I check the connection, page

  • 0

I have stackedwidget with multiple pages, at page one, I check the connection, page two, I check the availability of user, and so on for the rest of page(I have five), basically, other pages depend in the first page, if the connection failed or not, my problem is about other pages, even the connection made with out any errors, the second page can’t made its query, I am getting this error:

QSqlQuery::prepare: database not open

My slot:

void ConfSetup::setNextPage()
{
    int currentIndex = ui->stackedWidget->currentIndex();

    switch(currentIndex)
     {
       case 1:
        ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;

       case 2:
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setDatabaseName("mysql");
        db.setHostName(ui->serverEdit->text());
        db.setPort(ui->portEdit->text().toInt());
        db.setUserName(ui->userEdit->text());
        db.setPassword(ui->passwordEdit->text());

        if(!db.open())
         {
           QMessageBox::critical(0, trUtf8("Fail to login"), trUtf8("Wrong user or password"));
         }
        else
          ui->stackedWidget->setCurrentIndex(currentIndex + 1);

       break;

       case 3:
        query.prepare("SELECT user FROM user WHERE user=:user");
        query.bindValue(":user", ui->userDbEdit->text());
        query.exec();

        if(query.numRowsAffected() == 1)
           QMessageBox::critical(0, trUtf8("User exist"), trUtf8("This user already token"));
        else
           ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;

       case 4:
        query.prepare("SELECT SCHEMA_NAME AS 'Database' FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database");
        query.bindValue(":database", ui->dbNameEdit->text());
        query.exec();

        if(query.numRowsAffected() == 1)
           QMessageBox::critical(0, trUtf8("Fail to create database"), trUtf8("This database already exist"));
        else
           ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;
     }
}

The connection:

connect(ui->nextButton0, SIGNAL(clicked()), this, SLOT(setNextPage()));
connect(ui->nextButton1, SIGNAL(clicked()), this, SLOT(setNextPage()));
//etc
  • 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-22T13:08:32+00:00Added an answer on May 22, 2026 at 1:08 pm

    From the documentation:

    Warning: You must load the SQL driver
    and open the connection before a
    QSqlQuery is created. Also, the
    connection must remain open while the
    query exists; otherwise, the behavior
    of QSqlQuery is undefined.

    So I have to declare QSqlQuery query variable at each case statment, now my problem solved.

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

Sidebar

Related Questions

I have StackedWidget with 3 pages, every page contain 3 lineEdit, now at every
Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
We have a designer creating a user interface for an application. The main window
Have a bunch of WCF REST services hosted on Azure that access a SQL
have Googled the cr*p out of this one so apologies if the answer is
Have a look at one of my websites: moskah.com The problem is that it
Have posting working wonderfully, and reading the response happily EXCEPT when one of the
I am new to Qt creator. I have a stacked widget with 3 pages.
Have any one tried to activate fancybox thumbnail gallery using a button or an
Have you any idea how to find out if spell check suggestions dialog is

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.