OK, I can force the model to be a QStringListModel so I can sort it, but if I do, I seem to be losing the ability to assign data to the entries.
//
itemCombo.setWindowTitle("Item");
for (int i = 9; i >= 0; --i)
{
itemCombo.
addItem(QString::number(i
),
100 + i
);
}
for (int i = 0; i < itemCombo.count(); ++i)
{
cout << i << ' ' << itemCombo.itemText(i).toLatin1().data() << ' ';
cout << itemCombo.itemData(i).toInt();
cout << " [" << itemCombo.itemData(i).toString().toLatin1().data() << ']';
cout << endl;
}
//
// Clear the contents and try again, this time with
// a non-default model
//
itemCombo.clear();
for (int i = 9; i >= 0; --i)
{
itemCombo.
addItem(QString::number(i
),
100 + i
);
}
cout << "==============" << endl;
for (int i = 0; i < itemCombo.count(); ++i)
{
cout << i << ' ' << itemCombo.itemText(i).toLatin1().data() << ' ';
cout << itemCombo.itemData(i).toInt();
cout << " [" << itemCombo.itemData(i).toString().toLatin1().data() << ']';
cout << endl;
}
itemCombo.model()->sort(0);
itemCombo.show();
//
QComboBox itemCombo;
itemCombo.setWindowTitle("Item");
for (int i = 9; i >= 0; --i)
{
itemCombo.addItem(QString::number(i), 100 + i);
}
for (int i = 0; i < itemCombo.count(); ++i)
{
cout << i << ' ' << itemCombo.itemText(i).toLatin1().data() << ' ';
cout << itemCombo.itemData(i).toInt();
cout << " [" << itemCombo.itemData(i).toString().toLatin1().data() << ']';
cout << endl;
}
//
// Clear the contents and try again, this time with
// a non-default model
//
itemCombo.clear();
itemCombo.setModel(new QStringListModel());
for (int i = 9; i >= 0; --i)
{
itemCombo.addItem(QString::number(i), 100 + i);
}
cout << "==============" << endl;
for (int i = 0; i < itemCombo.count(); ++i)
{
cout << i << ' ' << itemCombo.itemText(i).toLatin1().data() << ' ';
cout << itemCombo.itemData(i).toInt();
cout << " [" << itemCombo.itemData(i).toString().toLatin1().data() << ']';
cout << endl;
}
itemCombo.model()->sort(0);
itemCombo.show();
To copy to clipboard, switch view to plain text mode
This produces the following output:
0 9 109 [109]
1 8 108 [108]
2 7 107 [107]
3 6 106 [106]
4 5 105 [105]
5 4 104 [104]
6 3 103 [103]
7 2 102 [102]
8 1 101 [101]
9 0 100 [100]
==============
0 9 0 []
1 8 0 []
2 7 0 []
3 6 0 []
4 5 0 []
5 4 0 []
6 3 0 []
7 2 0 []
8 1 0 []
9 0 0 []
0 9 109 [109]
1 8 108 [108]
2 7 107 [107]
3 6 106 [106]
4 5 105 [105]
5 4 104 [104]
6 3 103 [103]
7 2 102 [102]
8 1 101 [101]
9 0 100 [100]
==============
0 9 0 []
1 8 0 []
2 7 0 []
3 6 0 []
4 5 0 []
5 4 0 []
6 3 0 []
7 2 0 []
8 1 0 []
9 0 0 []
To copy to clipboard, switch view to plain text mode
I just checked in qstringlistmodel.cpp and it only returns data for Qt::DisplayRole and Qt::EditRole, not Qt::UserRole. Guess I get to write my own extension...
Bookmarks