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 7670929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:56:00+00:00 2026-05-31T15:56:00+00:00

I have recently begun working with QT and I’m having a small issue. I

  • 0

I have recently begun working with QT and I’m having a small issue. I cant seem to understand how QVariant works. I’ve been going through the help and looking online but it is just not sinking in. In my project i am trying to populate a combo box with a list of manufacturers from a database. I have opened the database and pulled out the entries but I rename them all manValue. Now I understand why this is happening, the problem is I do not understand how to properly use QVariant to get the result I want. Originally i thought “manValue” was going to be the identifier for the string that held the actual value from the database but instead it reads the value from the database and makes sure it is not null, and then renames it. I already tried making a string before I assign the QVariant with any properties and assigning it the text i received from the database, and then inserting that string where manValue is but still no luck. Any help would be greatly appreciated. Sorry, I know this is going to be simple I’m just a noob and the help docs often confuse me.

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LOCALHOST\\TestERPServer");
db.setDatabaseName("TestERPConnection");

if (db.open())
{
    QMessageBox::information(this,"Connected","Connection to the Database was Established\n"
                             "\nStatus: Connected");

    QSqlQuery mfrQry;

    if (mfrQry.exec("SELECT * FROM erp_data.manufacturers;"))
    {
        if (mfrQry.value(1) == "")
        {
            QMessageBox::information(this,"No Connection","Nothing in the Manufacturer Database\n"
                                     "\nError: " + db.lastError().text());
        }
        else
        {
            while (mfrQry.next())
            {
                ui->mfrComboBox->addItem("manValue",QVariant(mfrQry.value(1)));
            }
        }
    }
    else
    {
        QMessageBox::information(this,"No Connection","Connection to the Manufacturer Database could not be Established\n"
                                 "\nError: " + db.lastError().text());
    }
}
else
{
    QMessageBox::information(this,"No Connection","Connection to the Database could not be Established\n"
                             "\nError: " + db.lastError().text());
}
  • 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-31T15:56:01+00:00Added an answer on May 31, 2026 at 3:56 pm

    The first problem in the provided code has to do with the way you manipulate the QSqlQuery.

    Successfully executed SQL statements set the query’s state to active
    so that isActive() returns true. Otherwise the query’s state is set to
    inactive. In either case, when executing a new SQL statement, the
    query is positioned on an invalid record. An active query must be
    navigated to a valid record before values can be retrieved.

    In order to move to a valid record you have to use one of the following:

    • next()
    • previous()
    • first()
    • last()
    • seek()

    Once you are on a valid record you have to use the value() function in order to take the column you want. The returned value is QVariant so you need to convert to the desired type using one of the many toSomething functions QVariant provides.

    So in your case the code should look like this:

    QSqlQuery mfrQry;
    
    if (mfrQry.exec("SELECT * FROM erp_data.manufacturers;"))
    {
       // This will loop through all records returned by the query
       while (mfrQry.next()) {
             //  mfrQry.value(COLID) returns a QVariant containing the data of the current 
             // record in column COLID. 
             // Using toString we convert it to String
             QString stringValue = mfrQry.value(COLID).toString();
             // Now handle the QString the way you want...
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently begun working on a pre-existing PHP application, and am having a
I've recently begun a fairly small 2D project using SFML and TinyXML and have
I've recently begun my journey of learning CSS, and have been studying Gmail's design,
I have recently begun the process of trying to learn Ruby/Rails and have been
I have an issue where users on my web site have recently begun to
I have recently begun delving into the world that is database programming with C++
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 to use the EF v4 Code Only library for some
I have recently begun experiencing several odd issues with the Android SDK and I

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.