PDA

View Full Version : QString function by value



hmarani
23rd November 2010, 18:59
Newbie; the function below returns nothing(empty) but if I assign art="abc" returns abc what is wrong; please help.
Thanks
hmarani


QString Dialog::forsuly()
{

QSqlQueryModel model;
model.setQuery("SELECT * FROM art");
QString artn = model.record(4).value("artln").toString();
return artn
}

franz
23rd November 2010, 19:35
Just some possible issues based on your code:


Your record index could be wrong (QSqlMode::record() (http://doc.trolltech.com/latest/qsqlquerymodel.html#record) returns an empty result if the index is wrong)
The value "artln" might not exist (should it be artn?). (QSqlRecord::value() (http://doc.trolltech.com/latest/qsqlrecord.html#value-2) returns an invalid variant if the value name doesn't exist)

If necessary, check the results of QSqlQueryModel::lastError() (http://doc.trolltech.com/latest/qsqlquerymodel.html#lastError) to see what went wrong.

Also, please put your code in code tags.

hmarani
23rd November 2010, 19:41
Thanks for the reply; but I check the value by:


qDebug() << "in Dialog.cpp artistv: " + artn;

and I get the correct name. BTW, I am retrieving value from mysql and Qt 4.6

Lykurg
23rd November 2010, 21:15
then show us how you use the function. And if you just want to query a value, you better use a single QSqlQuery and don't abuse a model for that.

hmarani
23rd November 2010, 21:35
Thanks Lykurg, here is how I use the functon
QString alakyD=Dialog::forsuly();

which alakyD is a static QString in .h file and defined in .cpp. As I said if I use Artn="ABC" it returns Artn ABC
Thanks again for help.

wladek
24th November 2010, 09:03
Sorry for my off topic reply, but how many times does Lykurg need to edit your posts so you use [code] tags?

Wladek

Lykurg
24th November 2010, 10:01
With the informations you provide, I see no problem. So can you make a minimal compilable example reproducing your problem. (create a sql table in memory.)