Results 1 to 12 of 12

Thread: Populating a combobox with a record obtained from a database

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

    Question Sorting a models records using quicksort

    Ok so I have a database which contains a table called files. This file table contains a list of file names, file locations and the size of each file.

    I have managed to use a model to display the file names in a combobox. However, i want the file names to be populated in the combobox in decreasing order of file size. I have created a quicksort algorithm which is capable of sorting numbers , so how would i use the model and my algorithm to do what I require.... i don't even know where to start?! I have to use my own sorting algorithm as it's a requirement for the project I am doing.

    If someone could point me in the right direction I would be so grateful!

    Thanks very much for your time and trouble

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Sorting a models records using quicksort

    Have you looked at QSortFilterProxyModel?

    Then you probably need to reimplement QSortFilterProxyModel::lessThan() (in your QSortFilterProxyModel subclass) to return the correct value based on your algorithm.

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

    Default Re: Sorting a models records using quicksort

    Hmmm that seems pretty complex... are there any other ways? perhaps a work around which doesn't involve a model? I'm sure i'll think of a hacky way to do it eventually... i normally do, but it makes my code look rather messy

    Thanks and I'll keep you updated on what I do.

  4. #4
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Sorting a models records using quicksort

    perhaps a work around which doesn't involve a model?
    Then dont use model, use your own logic to populate the qcombobox.

    steps:
    1)use QSqlQuery to select the records from db.
    2)apply your QuickSort Alg
    3)populate your combobox based on QuickSort logic

    if you simply want to sort
    Qt Code:
    1. myCombo->model()->sort(0,Qt::DescendingOrder);
    To copy to clipboard, switch view to plain text mode 
    Note:
    you can also refer qSort(for containers)
    Bala

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

    Question Populating a combobox with a record obtained from a database

    Ok so I have created a record which looks like this:

    Qt Code:
    1. " 0:" QSqlField("FileID", int, length: 11, precision: 0, required: yes, generated: yes, typeID: 3) ""
    2. " 1:" QSqlField("File_Name", QString, length: 300, precision: 0, required: no, generated: yes, typeID: 253) ""
    To copy to clipboard, switch view to plain text mode 

    How do i go about populating the combo box with each of the values contained within the File_name column? I do not want to use a model! How would I go about sorting the data within this record? Say i wanted to sort the File_name in ascending order, with my quicksort algorithm, how would i apply my algorithm to this record?

    Thanks for your time and trouble

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Populating a combobox with a record obtained from a database

    You might want to try an sql model and a data widget mapper.

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

    Default Re: Populating a combobox with a record obtained from a database

    I'm trying to avoid the use of a model!
    How would i populate a combo box with records if I do not want to use a model?

    Thanks for your time and trouble!

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Populating a combobox with a record obtained from a database

    Just iterate on sql result and add each row to combobox. What a problem ?

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

    Default Re: Populating a combobox with a record obtained from a database

    Could you perhaps show me how to do this. Thanks

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

    Question Using quicksort to update a combobox

    I've been trying for hours now trying to get this to work and I think I have hit a brick wall. It's pretty frustrating!

    I have a database, with a table within it named files. I query this table with this query:

    Qt Code:
    1. SELECT f.FileID, f.File_Name FROM File f LEFT JOIN Stats s ON f.FileID = f.FileID AND s.UserID = "+ userid + " WHERE s.FileID IS NULL
    To copy to clipboard, switch view to plain text mode 

    I then want to sort the data by filename (using my quicksort) and populate a combo box with the sorted filenames

    My quicksort consists of this:

    quicksort.h
    Qt Code:
    1. #ifndef QUICKSORT_H
    2. #define QUICKSORT_H
    3.  
    4. class QuickSort
    5. {
    6. public:
    7. char *FileNames[1000];
    8. void QuSort(int left, int right);
    9. int partition(int left, int right);
    10. void swap(int a, int b);
    11.  
    12. };
    13.  
    14. #endif // QUICKSORT_H
    To copy to clipboard, switch view to plain text mode 

    quicksort.cpp
    Qt Code:
    1. #include "quicksort.h"
    2. #include <cstring>
    3.  
    4. using namespace std;
    5.  
    6. void QuickSort::QuSort(int left, int right)
    7. {
    8. int p;
    9.  
    10. if(left>=right)
    11. return;
    12. p = partition(left, right);
    13.  
    14. QuSort(left,p-1);
    15. QuSort(p+1,right);
    16. }
    17.  
    18. int QuickSort::partition(int left, int right)
    19. {
    20. int first,pivot;
    21.  
    22. first = left;
    23. pivot = right--;
    24. while(left<=right)
    25. {
    26. while(strcmp(FileNames[left],FileNames[pivot]) < 0)
    27. left++;
    28. while( (right>=first) && (strcmp(FileNames[right],FileNames[pivot])>=0) )
    29. right--;
    30. if(left<right)
    31. {
    32. swap(left,right);
    33. left++;
    34. }
    35. }
    36.  
    37. if(left!=pivot)
    38. swap(left,pivot);
    39.  
    40. return left;
    41. }
    42.  
    43. void QuickSort::swap(int a, int b)
    44. {
    45. char *temp;
    46.  
    47. temp=FileNames[a];
    48. FileNames[a]=FileNames[b];
    49. FileNames[b]=temp;
    50. }
    To copy to clipboard, switch view to plain text mode 


    Then when a user selects a filename from down box and presses a button I have another query which updates the database, this query looks like this:

    Qt Code:
    1. INSERT INTO stats (`UserID`, `FileID`, `Completed`) VALUES ('"+ userid +"', '"+fileid+"', 'No');
    To copy to clipboard, switch view to plain text mode 

    Finally the combo box is updated to remove the fileid which has been added to the table within the database.


    The problems I face are:
    1) I don't know how to sort the query results i get (with my quicksort) and keep the file names linked to the File Id
    2) I don't know how to populate the combo box with my the query results
    3) I don't know how to obtain the fileid of a sorted item highlighted in the combo box

    I'm required, by my lecturer, to use my own quicksort function, so i cannot use a function already avaliable to me.

    Please could someone help me

    Thanks very much for your time and trouble

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using quicksort to update a combobox

    First of all you don't need to implement your own quicksort as Qt already has it implemented in qSort. Second of all why don't you just modify your database query to return values in the proper order?

    BTW. I merged all your threads related to this issue into this one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using quicksort to update a combobox

    1. As I see You want to select from table File records without record in table Stats and result must be ordered by file name. So query should looks liek this
    Qt Code:
    1. SELECT FileID, File_Name FROM File WHERE FileID NOT IN (SELECT DISTINCT FileID FROM Stats) ORDER BY File_Name
    To copy to clipboard, switch view to plain text mode 
    2. Getting data from database and populating to combo box should looks something like this
    Qt Code:
    1. QSqlQuery query(yours_db);
    2. if( query.exec("SELECT FileID, File_Name FROM File WHERE FileID NOT IN (SELECT DISTINCT FileID FROM Stats) ORDER BY File_Name")
    3. {
    4. while( query.next() )
    5. {
    6. yours_combo_box.append(query.value(1).toString, query.value(0).toLong());
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    And RTFM.

Similar Threads

  1. Querying a database and populating a combobox
    By Splatify in forum Newbie
    Replies: 7
    Last Post: 21st February 2011, 11:28
  2. Database combobox search
    By poporacer in forum Newbie
    Replies: 10
    Last Post: 24th October 2010, 14:21
  3. Replies: 3
    Last Post: 4th August 2010, 18:51
  4. Replies: 2
    Last Post: 13th April 2010, 16:50
  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.