Can't get the SQL statement in this function to work. I've gone over and over it and rewritten it multiple times but just can't see what's wrong.

The function sets up a number of QActions for a popup menu which all works fine.
The commented lines are the new code added to retrieve the saved preferences from an SQLite database (sets whether actions are checked or not on launch) which is what's not working.

Every iteration of the for loop which calls userVars.value() errors with -
QSqlQuery::value: not positioned on a valid record.

Also debug of userVars.value(i) returns QVariant(,)

Heres the functions :

Qt Code:
  1. QMenu* Cust::f_cust_colFilterPopUp()
  2. {
  3. QSqlQuery userVars;
  4. QStringList temp_details;
  5. QString temp_checked;
  6. temp_details << "Customer Id"<< "Surname"<< "Name"<< "Address"<< "City"<< "State"<< "Postcode"
  7. << "Country"<< "Business ph.1"<< "Home ph.1"<< "Business ph.2"<< "Home ph.2"
  8. <<"Mobile"<< "Fax"<< "Scheduled Jobs"<< "Non Scheduled Jobs"<< "Incomplete Jobs"
  9. <<"Total Jobs"<< "Jobs to Invoice"<< "Total Invoiced"<< "Unpayed Invoices";
  10.  
  11.  
  12. //QString to replace * in SQL statement, using * for debuging atm
  13. /*temp_checked << "rowid" << "showSurname"<< "showName"<< "showAddress"<< "showCity"<<
  14.   "showState"<< "showPcode"<< "showCountry"<< "showBPhone1"<<
  15.   "showHPhone1"<< "showBPhone2"<< "showHPhone2"<< "showMobPhone"<<
  16.   "showFax"<< "showScheduledJobs"<< "showUnscheduledJobs"<<
  17.   "showIncompleteJobs"<< "showTotalJobs"<< "showToInvoice"<<
  18.   "showInvoiced"<< "showOutInvoice";*/
  19.  
  20.  
  21. if (!userVars.exec("SELECT * FROM savedData WHERE rowid=1")) //exec SQL and error check
  22. {
  23. QString error = userVars.lastError().text();
  24. QMessageBox::critical(0, qApp->tr("Query Failed"),
  25. qApp->tr("Unable to execute sql query.\n"
  26. + userVars.lastError().text().toAscii() +
  27. "\n Click cancel to exit."), QMessageBox::Cancel);
  28. }
  29.  
  30.  
  31. qmenu_cust_popUpMenu = new QMenu;
  32. for(int i=0;i<20;i++)
  33. {
  34. qaction_cust_popUpAction[i] = new QAction(qApp->activeWindow());
  35. qaction_cust_popUpAction[i]->setCheckable(true);
  36. qaction_cust_popUpAction[i]->setText(temp_details.at(i));
  37.  
  38. bool checked = userVars.value(i).toBool(); //get field and convert to bool
  39. qaction_cust_popUpAction[i]->setChecked(checked); //set checkbox value
  40.  
  41. qmenu_cust_popUpMenu->addAction(qaction_cust_popUpAction[i]);
  42. connect(qaction_cust_popUpAction[i], SIGNAL(changed()), this, SLOT(sl_cust_filter()));
  43.  
  44. qDebug()<<(userVars.value(i)); //debug returns QVariant(,)
  45. }
  46. return qmenu_cust_popUpMenu;
  47. }
To copy to clipboard, switch view to plain text mode