
Originally Posted by
guenthk
The contents of our combo box is changed and re-sorted again and again, depending on user actions. According to your proposal, the combo box model would be replaced then on every sort operation while the original model would be retained, too. So we would get an increasing number of models this way.
Well, sorry if I was unclear, but this is not what I meant. I meant that the proxy model would be applied only once.
As I have understood the model/view philosophy of Qt4 and of QSortFilterProxyModel, the correct procedure as to sorting of list views is to insert an intermediate QSortFilterProxyModel between the base/source model and the view without replacing the source model, and then to sort the QSortFilterProxyModel.
Yes, this is correct and also exactly the way I wanted it to work with combo box too..
I noticed something funny. Sorting items between insertions makes items disappear, indeed. The sorting seems to work only once or something. Try uncommenting the first line doing the sort.
#include <QtGui>
{
public:
SortableComboBox
(QWidget* parent
= 0) {
proxy->setSourceModel(model);
setModel(proxy);
}
};
int main(int argc, char* argv[])
{
SortableComboBox combo;
combo.addItem("B");
combo.addItem("C");
combo.addItem("A");
// combo.model()->sort(0); // <- uncomment this line and items disappear..
combo.addItem("F");
combo.addItem("E");
combo.addItem("D");
combo.model()->sort(0);
combo.show();
return a.exec();
}
#include <QtGui>
class SortableComboBox : public QComboBox
{
public:
SortableComboBox(QWidget* parent = 0)
: QComboBox(parent)
{
QStandardItemModel* model = new QStandardItemModel(0, 1, this);
QSortFilterProxyModel* proxy = new QSortFilterProxyModel(this);
proxy->setSourceModel(model);
setModel(proxy);
}
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
SortableComboBox combo;
combo.addItem("B");
combo.addItem("C");
combo.addItem("A");
// combo.model()->sort(0); // <- uncomment this line and items disappear..
combo.addItem("F");
combo.addItem("E");
combo.addItem("D");
combo.model()->sort(0);
combo.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks