c++ - Sorting QComboBox delete data in UserRole -
i have problem custom data in qcombo box , here minimal example, empty project qcombobox named combobox in ui. here c'tor code:
ui->setupui(this); //add sort proxy qsortfilterproxymodel* proxy = new qsortfilterproxymodel(ui->combobox); proxy->setsourcemodel(ui->combobox->model()); ui->combobox->model()->setparent(proxy); ui->combobox->setmodel(proxy); //first strange thing //here first additem triggers current index changed signal //in second run, every additem trigger signal qdebug() << "initializing"; for( size_t i=1; i<1e10; i*=10 ) ui->combobox->additem( "item", ); qdebug() << "sorting"; proxy->sort(0); proxy->sort(0); qdebug() << "selecting"; ui->combobox->setcurrentindex(0); //here fine for( int i=0; i<ui->combobox->count(); ++i ) qdebug() << ui->combobox->itemdata(i).value<size_t>(); //clear box , same again! ui->combobox->clear(); //now every additem triggers current changed event qdebug() << "initializing"; for( size_t i=1; i<1e10; i*=10 ) ui->combobox->additem( "item", ); qdebug() << "sorting"; proxy->sort(0); qdebug() << "selecting"; ui->combobox->setcurrentindex(0); //here itemdata values 0!!! for( int i=0; i<ui->combobox->count(); ++i ) qdebug() << ui->combobox->itemdata(i).value<size_t>();
and here slot
void mainwindow::on_combobox_currentindexchanged(int index) { qdebug() << "current changed " << index; qdebug() << "userrole " << ui->combobox->itemdata(index); }
the second time add items calls current index changed event every time, first time on first additem.
in second run, after sort, user data 0.
any ideas?
this (http://www.qtcentre.org/threads/3741-how-to-sort-a-qcombobox-in-qt4) post suggest bug. 10 years old know im getting same error. if remove first sort(0) data doesn't disappear. maybe make custom insert function items insert in correct order.
ui->combobox->setinsertpolicy(qcombobox::insertalphabetically);
but lessthan behaviour.
Comments
Post a Comment