Results 1 to 8 of 8

Thread: Querying a database and populating a combobox

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question 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:

    Qt Code:
    1. 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";
    To copy to clipboard, switch view to plain text mode 


    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  3. #3
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  4. #4
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  5. The following user says thank you to kornicameister for this useful post:

    Splatify (21st February 2011)

  6. #5
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Querying a database and populating a combobox

    Ok so this is what I have done so far:

    Qt Code:
    1. 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";
    2.  
    3. QSqlQuery modelquery;
    4.  
    5. modelquery.exec(querystring);
    6. model->setQuery(modelquery);
    7. model->setHeaderData(0, Qt::Horizontal, "UserID");
    8. model->setHeaderData(1, Qt::Horizontal, "File Name");
    9. model->setHeaderData(2, Qt::Horizontal, "File Location");
    10. model->setHeaderData(3, Qt::Horizontal, "Size");
    11. model->setHeaderData(4, Qt::Horizontal, "Finished");
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. ui->ComboBox->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    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

  7. The following user says thank you to Splatify for this useful post:

    wiz (16th January 2012)

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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().

  9. The following user says thank you to Lykurg for this useful post:

    Splatify (21st February 2011)

  10. #7
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Querying a database and populating a combobox

    Ok so now i have this:

    Qt Code:
    1. ui->QuizBox->setModel(model);
    2. ui->QuizBox->setModelColumn(1);
    To copy to clipboard, switch view to plain text mode 


    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

  11. #8
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Querying a database and populating a combobox

    Ok so i i think i know how to find the current index. I'm using:
    Qt Code:
    1. ui->ComboBox->currentIndex();
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QSqlRecord record = model->record(ui->ComboBox->currentIndex());
    2. QString filelocation;
    3. filelocation = record.value("File_Location").toString();
    To copy to clipboard, switch view to plain text mode 

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

    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
    Last edited by Splatify; 21st February 2011 at 11:28.

Similar Threads

  1. Database combobox search
    By poporacer in forum Newbie
    Replies: 10
    Last Post: 24th October 2010, 14:21
  2. Populating, signals and comboboxes
    By ShamusVW in forum Newbie
    Replies: 6
    Last Post: 12th August 2010, 06:43
  3. Querying object type using itemAt
    By ajb in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 16:35
  4. Querying within Threads
    By bera82 in forum Qt Programming
    Replies: 11
    Last Post: 12th March 2008, 01:08
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.