[Qt-interest] QTreeView custom sorted columns

Robert Reif reif at earthlink.net
Wed Nov 25 06:14:28 CET 2009


I'm new to QT and am trying to modify an existing application (cppcheck 
gui) to sort the QTreeView column containing line numbers (2) properly.

I'm doing this using a QSortFilterProxyModel with an overridden 
lessThan.  The sorting woks but I get the following error repeated many 
times when running it:

QSortFilterProxyModel: index from wrong model passed to mapToSource

Here is the relevant chunk of modified code:

class MySortFilterProxyModel : public QSortFilterProxyModel
{
public:
    bool lessThan(const QModelIndex &left,
                  const QModelIndex &right) const;
};

bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
                                      const QModelIndex &right) const
{
     QVariant leftData = sourceModel()->data(left);
     QVariant rightData = sourceModel()->data(right);

     if (left.column() == 2)
         return leftData.toInt() < rightData.toInt();

    return leftData.toString() < rightData.toString();
}

ResultsTree::ResultsTree(QWidget * parent) :
        QTreeView(parent),
        mContextItem(0),
        mCheckPath(""),
        mVisibleErrors(false)
{
    for (int i = 0; i < SHOW_NONE; i++)
        mShowTypes[i] = false;

    mModel = new QStandardItemModel;
#if 0
    setModel(mModel);
#else
    MySortFilterProxyModel * model = new MySortFilterProxyModel();
    model->setSourceModel(mModel);
    setModel(model);
#endif

    QStringList labels;
    labels << tr("File") << tr("Severity") << tr("Line") << tr("Message");
    mModel->setHorizontalHeaderLabels(labels);
    setExpandsOnDoubleClick(false);
    setSortingEnabled(true);

    connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
            this, SLOT(QuickStartApplication(const QModelIndex &)));
}

ResultTree is defined as:

class ResultsTree : public QTreeView

I'm using QT 4.5 from ubuntu 9.10.

What am I doing wrong?




More information about the Qt-interest-old mailing list