Hi,
I want to sort a QComboBox locale aware, so I used a reimplementation of QSortFilterProxyModel. Unfortunately, it messes up the contents of the QComboBox if I insert Items after calling MyCombo->model()->sort(0);, which does not happen if I just use QSortFilterProxyModel without reimplementing it, so I guess my reimplementation is "naive" and something is missing/wrong.
In the example (stripped to the important stuff, but is compilable) I add A, C, B to the ComboBox, sort it, then I add D and the ComboBox shows <empty line>, A, B, D - and C is missing. Has anybody an idea what I did wrong reimplemnting QSortFilterProxyModel?
Thanks for your help
Armin
Files: (attached also as zip)
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mysortfilterproxymodel.h
#ifndef MYSORTFILTERPROXYMODEL_H
#define MYSORTFILTERPROXYMODEL_H
#include <QtGUI>
{
Q_OBJECT
public:
MySortFilterProxyModel
(QObject *parent
);
};
#endif // MYSORTFILTERPROXYMODEL_H
#ifndef MYSORTFILTERPROXYMODEL_H
#define MYSORTFILTERPROXYMODEL_H
#include <QtGUI>
class MySortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
MySortFilterProxyModel(QObject *parent);
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
};
#endif // MYSORTFILTERPROXYMODEL_H
To copy to clipboard, switch view to plain text mode
mysortfilterproxymodel.cpp
#include "mysortfilterproxymodel.h"
{
}
//---------------------------------------------------------------------------
bool MySortFilterProxyModel
::lessThan(const QModelIndex &left,
{
QVariant leftData
= sourceModel
()->data
(left
);
QVariant rightData
= sourceModel
()->data
(right
);
QString leftString
= leftData.
toString();
QString rightString
= rightData.
toString();
return QString::localeAwareCompare(leftString, rightString
) <
0;
}
#include "mysortfilterproxymodel.h"
MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{
}
//---------------------------------------------------------------------------
bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
const QModelIndex &right) const
{
QVariant leftData = sourceModel()->data(left);
QVariant rightData = sourceModel()->data(right);
QString leftString = leftData.toString();
QString rightString = rightData.toString();
return QString::localeAwareCompare(leftString, rightString) < 0;
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGUI>
#include "mysortfilterproxymodel.h"
Q_OBJECT
public:
~MainWindow();
MySortFilterProxyModel *MyProxySortModel;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGUI>
#include "mysortfilterproxymodel.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QComboBox *MyCombo;
MySortFilterProxyModel *MyProxySortModel;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
{
MySortFilterProxyModel *MyProxySortModel = new MySortFilterProxyModel(MyCombo);
MyProxySortModel->setSourceModel(MyCombo->model());
MyCombo->model()->setParent(MyProxySortModel);
MyCombo->setModel(MyProxySortModel);
MyCombo->addItem("A", 1);
MyCombo->addItem("C", 3);
MyCombo->addItem("B", 2);
MyCombo->model()->sort(0);
MyCombo->addItem("D", 4);
}
MainWindow::~MainWindow() {}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
MyCombo = new QComboBox(this);
MySortFilterProxyModel *MyProxySortModel = new MySortFilterProxyModel(MyCombo);
MyProxySortModel->setSourceModel(MyCombo->model());
MyCombo->model()->setParent(MyProxySortModel);
MyCombo->setModel(MyProxySortModel);
MyCombo->addItem("A", 1);
MyCombo->addItem("C", 3);
MyCombo->addItem("B", 2);
MyCombo->model()->sort(0);
MyCombo->addItem("D", 4);
}
MainWindow::~MainWindow() {}
To copy to clipboard, switch view to plain text mode
comboproxy.zip
Bookmarks