PDA

View Full Version : Can't get Icons in vertical header with QSortFilterProxyModel



derrickbj
23rd November 2013, 02:58
Hello,

I'm working on a program that reads in a text file and puts data into a QStandardItemModel attached to a QTableView. If a particular word appears in a line of text read from the file, the program will put an "X" icon in the vertical header for that particular row. I'm having a problem with this X appearing in a QSortFilterProxyModel.

Everything works fine in standard form. However, I'm implementing filtering capability on the tableview with a 2 QLineEdit input fields above the table. I connect the two lineEdits to a filterChanged() slot and I reimplemented QSortFilterProxyModel as follows:

mysortfilterproxymodel.h:


#ifndef MYSORTFILTERPROXYMODEL_H
#define MYSORTFILTERPROXYMODEL_H

#include <QSortFilterProxyModel>

class MySortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit MySortFilterProxyModel(QObject *parent = 0);
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;

protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};

#endif // MYSORTFILTERPROXYMODEL_H


mysortfilterproxymodel.cpp:


#include "mysortfilterproxymodel.h"
#include <QIcon>

MySortFilterProxyModel::MtSortFilterProxyModel(QOb ject *parent) :
QSortFilterProxyModel(parent)
{
}
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{

QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
//filtering on 2 columns

QString filt1 = filterRegExp().pattern().section("|",0,0);
QString filt2 = filterRegExp().pattern().section("|",1,1);

QRegExp f1(filt1);
QRegExp f2(filt2);

return (sourceModel()->data(index0).toString().contains(f1)
&& sourceModel()->data(index1).toString().contains(f2));
}

QVariant MySortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical){
if (role == Qt::DecorationRole)
return QIcon(":MyProgram/images/x.png");
return QSortFilterProxyModel::headerData(section, orientation, role);
}
return QSortFilterProxyModel::headerData(section, orientation, role);
}


My filterChanged() slot is as follows:



void MainWindow::filterChanged(const QString &filt)
{
QString toFilt = filt;
QString filt1, filt2= "";

filt1 = ui->colOneFilterLineEdit->text();
filt2 = ui->colTwoFilterLineEdit->text();
toFilt = filt1 + "|" + filt2;

myProxyModel->setFilterFixedString(toFilt);
}


Here is the model setup and the line of code that detects the particular word and then places the X in the vertical header:




parseModel = new QStandardItemModel(0, 2, this);
parseModel ->setHeaderData(0, Qt::Horizontal, "Direction");
parseModel ->setHeaderData(1, Qt::Horizontal, "Street");

//filtering proxy model for parsing table model
proxyModel = new MySortFilterProxyModel;
proxyModel->setSourceModel(parseModel);

// ... Code to read in file line by line and parse each line into the model. If the "word" is found, set wordPresent to true ...

if (wordPresent){
parseModel->setHeaderData(row,Qt::Vertical,QIcon(":MyProgram/images/x.png"),Qt::DecorationRole);
}


The problem I'm having is that the line of code that checks for the particular word and then places an X in the vertical column works fine when there's no QSortFilterProxyModel. That is to say the X shows up fine in the vertical header. Once I attach the proxy model, the X goes away. So I did some Google searches and saw suggestions to implement QSortFilterProxyModel::headerData(), which I tried above. But this winds up placing an X on every single row as well as a row number, so I don't think I'm doing it properly. I also tried returning a QVariant() when no Vertical or DecorationRole was detected in the mysortfilterproxymodel, but that essentially erased the vertical header.

Can anybody help me understand where I'm going wrong and provide a suggestion to right the ship??? Thanks so much!

ChrisW67
24th November 2013, 22:25
Your headerData() function (line 30) does not qualify on section (i.e. column) and simply returns the icon for every column's Qt::DecorationRole. You will need to qualify on columns that contain matches and return a null QVariant for the others.

derrickbj
26th November 2013, 15:58
Ok, that makes perfect sense. However, I'm having problems qualifying the headerData(). As I mentioned earlier, the program reads in lines of text and if the text contains a specific word, it places the icon in the vertical header for that particular row. This works fine without the proxy model, but disappears when using the proxy model. So in MySortFilterProxyModel::headerData(), I'm attempting to check for !isNull() as the qualifier, but still no icon gets returned.


QVariant MySortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical){
if (role == Qt::DecorationRole){
if (!(sourceModel()->headerData(section,orientation,role)).isNull()){
return QIcon(":MyProgram/images/x.png");
}
else return QVariant();
}
}
return QSortFilterProxyModel::headerData(section, orientation, role);
}