PDA

View Full Version : Populating a combobox with a record obtained from a database



Splatify
23rd February 2011, 10:54
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

helloworld
23rd February 2011, 11:55
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.

Splatify
23rd February 2011, 12:26
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 :eek:

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

BalaQT
23rd February 2011, 14:52
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

myCombo->model()->sort(0,Qt::DescendingOrder);
Note:
you can also refer qSort(for containers)
Bala

Splatify
23rd February 2011, 21:27
Ok so I have created a record which looks like this:


QSqlRecord( 2 )
" 0:" QSqlField("FileID", int, length: 11, precision: 0, required: yes, generated: yes, typeID: 3) ""
" 1:" QSqlField("File_Name", QString, length: 300, precision: 0, required: no, generated: yes, typeID: 253) ""

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

tbscope
24th February 2011, 07:00
You might want to try an sql model and a data widget mapper.

Splatify
24th February 2011, 09:45
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!

Lesiok
24th February 2011, 11:54
Just iterate on sql result and add each row to combobox. What a problem ?

Splatify
24th February 2011, 14:11
Could you perhaps show me how to do this. Thanks

Splatify
24th February 2011, 14:53
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:


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

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


#ifndef QUICKSORT_H
#define QUICKSORT_H

class QuickSort
{
public:
char *FileNames[1000];
void QuSort(int left, int right);
int partition(int left, int right);
void swap(int a, int b);

};

#endif // QUICKSORT_H


quicksort.cpp

#include "quicksort.h"
#include <cstring>

using namespace std;

void QuickSort::QuSort(int left, int right)
{
int p;

if(left>=right)
return;
p = partition(left, right);

QuSort(left,p-1);
QuSort(p+1,right);
}

int QuickSort::partition(int left, int right)
{
int first,pivot;

first = left;
pivot = right--;
while(left<=right)
{
while(strcmp(FileNames[left],FileNames[pivot]) < 0)
left++;
while( (right>=first) && (strcmp(FileNames[right],FileNames[pivot])>=0) )
right--;
if(left<right)
{
swap(left,right);
left++;
}
}

if(left!=pivot)
swap(left,pivot);

return left;
}

void QuickSort::swap(int a, int b)
{
char *temp;

temp=FileNames[a];
FileNames[a]=FileNames[b];
FileNames[b]=temp;
}



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:


INSERT INTO stats (`UserID`, `FileID`, `Completed`) VALUES ('"+ userid +"', '"+fileid+"', 'No');

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

wysota
24th February 2011, 16:06
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.

Lesiok
24th February 2011, 19:51
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

SELECT FileID, File_Name FROM File WHERE FileID NOT IN (SELECT DISTINCT FileID FROM Stats) ORDER BY File_Name
2. Getting data from database and populating to combo box should looks something like this

QSqlQuery query(yours_db);
if( query.exec("SELECT FileID, File_Name FROM File WHERE FileID NOT IN (SELECT DISTINCT FileID FROM Stats) ORDER BY File_Name")
{
while( query.next() )
{
yours_combo_box.append(query.value(1).toString, query.value(0).toLong());
}
}
And RTFM.