Question

so here's what I'm working on. I have this software I'm working on with a main window, and an action that launches the following managaAttributesDialog Dialog class. When the dialog opens I execute a query on the database and create a list of a custom data type object called EAVFittmentProperties which just holds a few methods for getting and setting the properties which are and int for the index of the database and a QString which holds the name associated with that index. Everything seems to work fine, until I call setUpgGui, to turn those EAVFittmentPropeties into comboboxes which will hold there available values. It's meant to be a make model year search system so I can store and retrieve the paint colors for any given vehicle, so the EAVFitmentProperties hold 3 values right now; Make, Model, and Year. And these combo boxes will have the available make models and years in their lists. Once I call the setUpGui function and start to iterate through the list to create the combo boxes, it seems to skip the first value, only creating the model and year boxes.

I'm very confused as to what is happening to the first item in my list, so I have many Debug statements, and even run the same iteration just before the setUpGui function is called and everything works fine. Can anyone provide any insight, or have any idea what is going on? if there is any more info I can provide please ask, this is driving me crazy and I would love to know what is happening. Thanks in advance guys!

#include "manageattributesdialog.h"
#include "eavfittmentproperties.h"

#include <QtWidgets>
#include <QtSql>

ManageAttributesDialog::ManageAttributesDialog(QWidget *parent) :
QDialog(parent)
{
QSqlDatabase localdb = QSqlDatabase::database("");
if(localdb.open())
{
    QMessageBox::information(this,"Connected","Connection to the Database was Established\n"
                             "\nStatus: Connected");

    QSqlQuery *qry = new QSqlQuery(localdb);

    if(qry->exec("SELECT * FROM [ProductHelper].[dbo].[EAV_FittmentProperties]"))
    {
        int z = 1;
        while(qry->next())
        {
            z++;
            qDebug() << qry->value(0).toString();
            qDebug() << qry->value(1).toString();

            int mX = qry->value(0).toInt();

            EAVFittmentProperties *fitmentPropertyOption = new EAVFittmentProperties(this);
            fitmentPropertyOption->setIndexID(qry->value(0).toInt());
            fitmentPropertyOption->setName(qry->value(1).toString());

            fitmentPropertiesList.append(fitmentPropertyOption);

        }
        qry->finish();
    }
    else
    {
        QMessageBox::information(this,"Database Query Failed","Query to the Database could not be completed\n"
                                 "\nStatus: Query Not Completed\nError: " + localdb.lastError().text());
    }


    localdb.close();

    for(int i = 0; i < fitmentPropertiesList.count(); i++)
    {
        qDebug() << i << " is the number we are on";
        qDebug() << fitmentPropertiesList[i]->getIndexID();
        qDebug() << fitmentPropertiesList[i]->getName();
    }

}
else
{
    QMessageBox::information(this,"Not Connected","Connection to the Database could not be Established\n"
                             "\nStatus: Not Connected\nError: " + localdb.lastError().text());
}

setUpGui();

}

void ManageAttributesDialog::setUpGui()
{
//QFrame *mFrame = new QFrame(this);
QVBoxLayout *mLayout = new QVBoxLayout();

QLabel fitmentHeadingLbl;
fitmentHeadingLbl.setText(tr("Fitment Options"));

mLayout->addWidget(&fitmentHeadingLbl);
fitmentPropertiesList.begin();

qDebug() << fitmentPropertiesList.count();

for(int j = 0; j < fitmentPropertiesList.count(); j++)
{
    qDebug() << j << " is the number we are on";
    qDebug() << fitmentPropertiesList[j]->getIndexID();
    int sInt = fitmentPropertiesList[j]->getIndexID();
    qDebug() << fitmentPropertiesList[j]->getName();
    QString sString = fitmentPropertiesList[j]->getName().toLatin1();
    qDebug() << sInt;
    qDebug() << sString;
    fitmentPropertyLayout[j] = new QVBoxLayout();
    QVBoxLayout *thisLayout = fitmentPropertyLayout[j];
    fitmentPropertyLabel[j] = new QLabel();
    QString sString = fitmentPropertiesList[j]->getName().toLatin1();
    qDebug() << sString;
    fitmentPropertyLabel[j]->setText(sString);
    fitmentPropertyCombo[j] = new QComboBox();
    thisLayout->addWidget(fitmentPropertyLabel[j]);
    thisLayout->addWidget(fitmentPropertyCombo[j]);

    //mLayout->addLayout(thisLayout);
}

//mFrame->setLayout(mLayout);
this->setMinimumSize(400,400);
this->setLayout(mLayout);
}
Was it helpful?

Solution

Not using QSqlQuery, I just looked at this page: http://qt-project.org/doc/qt-4.8/qsqlquery.html

According to that page, you should use the first() function before entering the loop, and rewrite your loop this way:

bool bKeepLooping = qry->first();
while( keepLooping )
{
  // do stuff with the query
  // get next item at the bottom of the loop
  keepLooping = qry->next();
}

I am not a Qt expert. I'm just reading the docs and giving advice on what I believe is the issue. Most iterative approaches to getting "first" and "next" items in a list is done this way (reading files in a directory, getting items in a result set, etc.).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top