Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't get Icons in vertical header with QSortFilterProxyModel
Qt 6.11 is out! See what's new in the release blog

Can't get Icons in vertical header with QSortFilterProxyModel

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 972 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kewljohny
    wrote on last edited by
    #1

    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(QObject *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:
    @
    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!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved