PDA

View Full Version : Sorting Combobox Items



raphaelf
18th June 2008, 14:18
Hi,

im inserting some items like this:


QString path = ui.source_path_cb->currentText();
QDir dir(path);
QStringList list = dir.entryList(QDir::Files);
for(unsigned i=0; i<list.count();i++){
if(list[i]=="." || list[i]=="..") continue;
ui.software_cb->insertItem(-1, list[i]);}


How could i sort my combobox?

:crying:

jpn
18th June 2008, 14:30
See QComboBox::InsertPolicy.

Edit: Oh, and there is also a filter called QDir::NoDotAndDotDot to skip those entries in the entry list...

raphaelf
19th June 2008, 13:20
Hi,

i tried all InsertPolicy options showing in the designer (for example insert Alphabeticaly ...), but i have the false sorting..

w....a and not from a ... to w :crying:

raphaelf
20th June 2008, 07:20
Can somebody help me to sort my combobox :crying:

jpn
22nd June 2008, 18:00
You know, it's kind of impossible to say what's the problem without seeing any code. Anyway, here's two ways of listing files in an alphabetic order.

1) using QComboBox::InsertAlphabetically:


#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QComboBox combo;
combo.setInsertPolicy(QComboBox::InsertAlphabetica lly);
combo.addItems(QDir("/home/jp").entryList(QDir::Files | QDir::NoDotAndDotDot));
combo.show();
return app.exec();
}


2) using QDir::Name sort flag


#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QComboBox combo;
combo.addItems(QDir("/home/jp").entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name));
combo.show();
return app.exec();
}

raphaelf
23rd June 2008, 08:00
Hi JPN,

thank you very much!! It works :)

I only tried to set the option in the designer "insert Alphabeticaly"..

Thank u!!