Querying a database and populating a combobox
Ok so what i want to do is populate a combobox with the results from a query.
I have my query which is:
Code:
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
Re: Querying a database and populating a combobox
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.
Re: Querying a database and populating a combobox
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 :)
Re: Querying a database and populating a combobox
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
Re: Querying a database and populating a combobox
Ok so this is what I have done so far:
Code:
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";
modelquery.exec(querystring);
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:
Code:
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 :)
Re: Querying a database and populating a combobox
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().
Re: Querying a database and populating a combobox
Ok so now i have this:
Code:
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
Re: Querying a database and populating a combobox
Ok so i i think i know how to find the current index. I'm using:
Code:
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:
Code:
QSqlRecord record
= model
->record
(ui
->ComboBox
->currentIndex
());
filelocation = record.value("File_Location").toString();
The saying
Quote:
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