PDA

View Full Version : Querying a database and populating a combobox



Splatify
20th February 2011, 12:07
Ok so what i want to do is populate a combobox with the results from a query.
I have my query which is:


QString querystring = "SELECT u.UserID, f.FileName, f.FileLocation, s.Size, s.Finished FROM users u JOIN stats s ON s.UserID = u.UserID JOIN file f ON s.FileID = Q.FileID WHERE u.UserID = 1";


What I want to do is populate a combobox with the FileName and when a button is pressed, the selected file within the comobobox is loaded. How would i go about doing this? I can populate the combobox with all the FileNames, but i don't know how to load the file based on this. How do i pass the filelocation onto the relevant function? Could someone please help?

Thanks for your time and trouble

Lykurg
20th February 2011, 12:51
Use a QSqlQueryModel and set it via QComboBox::setModel() to your combobox. Then you can get all the information of the query through the normal index approach.

Splatify
20th February 2011, 13:00
Could you please give me an example of how to do this.... I've been messing about for hours now and I don't seem to be getting anywhere.

Thanks for your time and trouble :)

kornicameister
20th February 2011, 13:33
setModel method of QComboBox is reimplemented, it's argument is QAbstractItemModel
do as Lykurg suggested you to do, and simple use setModel to set QSqlQueryModel model for combo box

check the documentation of QSqlQueryModel if you do not know which method use to set the query

Splatify
20th February 2011, 13:44
Ok so this is what I have done so far:




QString querystring = "SELECT u.UserID, f.FileName, f.FileLocation, s.Size, s.Finished FROM users u JOIN stats s ON s.UserID = u.UserID JOIN file f ON s.FileID = Q.FileID WHERE u.UserID = 1";

QSqlQuery modelquery;

modelquery.exec(querystring);
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery(modelquery);
model->setHeaderData(0, Qt::Horizontal, "UserID");
model->setHeaderData(1, Qt::Horizontal, "File Name");
model->setHeaderData(2, Qt::Horizontal, "File Location");
model->setHeaderData(3, Qt::Horizontal, "Size");
model->setHeaderData(4, Qt::Horizontal, "Finished");

so then i have to update my combobox with this model. I can do this by doing:


ui->ComboBox->setModel(model);

However this puts the UserID in the combo box. How do i show the Filename in the combo box and then how do i use the model so when a particular file is selected within the combobox, the file location of the selected file is used in another function.

Thanks for your time and trouble :)

Lykurg
20th February 2011, 17:08
See the modelColumn property or simply query the filename as the first element in your query. Further make the model a member variable then you can access it in the slot and get the value by using QSqlQueryModel::record().

Splatify
20th February 2011, 19:00
Ok so now i have this:


ui->QuizBox->setModel(model);
ui->QuizBox->setModelColumn(1);




I'm still unsure of how to use this model. Could you give me some example code of how to use the model when an item is selected in the combobox and a button is pressed.

Thanks very much

Splatify
21st February 2011, 11:28
Ok so i i think i know how to find the current index. I'm using:

ui->ComboBox->currentIndex();

How do i go about accessing a record at this index? For example say i wanted to access the file location which is in column 2 and then store it into a string variable of filelocation.

Sorry for being such a pain but for some reasons this model business is really confusing me. :(

Thanks, once again, for your time and trouble.

Added after 47 minutes:

Hahah I think I may have got this to work again :)

This is what I did:


QSqlRecord record = model->record(ui->ComboBox->currentIndex());
QString filelocation;
filelocation = record.value("File_Location").toString();

The saying
If at first you don't suceeed, try, try and try again seems very apt here :o

Thanks very much all of you for your help. No doubt I will need some more help in the near future, I'm sure i'll be keeping you pros busy :p