QSortFilterProxyModel - additional column does not alternate row color



  • I have implemented a custom treeview displaying alternate color using a custom QSortFilterProxyModel. I override the column count and set it to 4 (one more then the original base class). During runtime, I see the additional row but the additional row does not comply with alternate color row schema. Is this a bug or am I forgetting to process some overridable display function to get this to behave properly?

    Thank you for your time and consideration!



  • This can't be answered withou looking at the code. It could have several reasons.

    My first guess would be the data function in you custom proxy model.



  • @
    test::test(QObject *parent) : QSortFilterProxyModel(parent)
    {
    }

    Qt::ItemFlags test::flags(const QModelIndex &index) const
    {
    if (!index.isValid())
    return Qt::NoItemFlags;

    QModelIndex sourceIndex = mapToSource(index);
    Qt::ItemFlags flags = sourceIndex.flags();
    if (index.column() == 0) {
        flags |= Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled |
                 Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
        if (sourceIndex.model()->hasChildren(sourceIndex))
        {
            flags |= Qt::ItemIsTristate;
        }
    }
    
    if (index.column() == 4) {
        flags |= Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    }
    return flags;
    

    }

    void test::setSourceModel(QAbstractItemModel *sourceModel)
    {
    if (sourceModel == QSortFilterProxyModel::sourceModel())
    return;

    QSortFilterProxyModel::setSourceModel(sourceModel);
    //m_checkStates.clear();
    

    }

    QVariant test::data(const QModelIndex &index, int role) const
    {
    QModelIndex sourceIndex = mapToSource(index);

    if (index.column() == 0 && role == Qt::CheckStateRole) {
        return QVariant(true);
    }
    else if (index.column() == 4)
        return "test column";
    
    return sourceIndex.data(role);
    

    }

    bool test::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    QModelIndex sourceIndex = mapToSource(index);

    if (index.column() == 0 && role == Qt::CheckStateRole) {
        Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
        return true;
    }
    
    return QSortFilterProxyModel::sourceModel()->setData(sourceIndex, value, role);
    

    }
    int test::columnCount ( const QModelIndex &parent = QModelIndex() ) const
    {
    return 5;
    }

    QVariant test::headerData(int section, Qt::Orientation orientation, int role) const
    {
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal && section == 4)
    return QVariant("Result");

    return QSortFilterProxyModel::headerData(section, orientation, role);
    

    }
    @



  • [quote author="phamtv" date="1309982616"]I have implemented a custom treeview displaying alternate color using a custom QSortFilterProxyModel. I override the column count and set it to 4 (one more then the original base class). During runtime, I see the additional row but the additional row does not comply with alternate color row schema. Is this a bug or am I forgetting to process some overridable display function to get this to behave properly? [/quote]

    Are you talking about rows or columns?
    In the text you say row, then column, then row.
    You say you set column count to 4 but set it to 5...

    Where do you do the alternate color? by the model? By the view?
    Where is your custom tree view?



  • sorry gerolf... my issues is dealing with the columns and the number is actually 4... I was changing it around to see if it made any difference. To answer your questions, I am alternating the color in the view. My calling function is as follow:

    @
    fsModel = new QFileSystemModel(this);
    fsModel->setRootPath(configDialog->strRoadTestDir);
    fsModel->setNameFilterDisables(false);
    QStringList myFilter;
    myFilter << "*.exp";
    fsModel->setNameFilters(myFilter);
    fsModel->setReadOnly(false);

    m_test = new test(this);
    m_test->setSourceModel(fsModel);
    
    fileTree = new QTreeView();
    fileTree->setModel(m_test);
    fileTree->setAlternatingRowColors(true);
    fileTree->setAnimated(true);
    fileTree->setSortingEnabled(true);
    fileTree->setContextMenuPolicy(Qt::CustomContextMenu);
    
    QModelIndex idx = fsModel->index(configDialog->strRoadTestDir);
    fileTree->setRootIndex(m_checkProxy->mapFromSource(idx));
    fileTree->sortByColumn(0, Qt::AscendingOrder);
    

    @



  • The problem might be, that your model does not return valid data for all queries for the cutomly added row. You would have to overwrite all data roles for read. Also all other calls (rowCount, columnCount etc)call mapProxyToSource which returns an invalid model index. This has impact on the returned data.



  • I have been multi-tasking with different projects. Now that I am back to this Qt project, I still find myself stuck with this issues. I just don´t understand how alternating color for a treeview is affected by adding a new column in my model. What is also puzzling is that in my test::data module below, I never hit the @else if (index.column() == 4)@ when clearly I have set the number of column to 5 and can see it during execution. And if I change the statement to @else if (index.column() == 3)@ then I get the text for my column. Any input is greatly appreciated.

    @
    QVariant test::data(const QModelIndex &index;, int role) const
    {
    QModelIndex sourceIndex = mapToSource(index);

    if (index.column() == 0 && role == Qt::CheckStateRole) {
        return QVariant(true);
    }
    else if (index.column() == 4 && role == Qt::DisplayRole)
        return QVariant("test column");
    
    return sourceIndex.data(role);
    

    }
    @

    Is it possible that because I set my souce model to a QFileSystemModel that I am limited to the # of column I can manipulate?



  • Did you ever find a solution to this? I need to customize QFileOpen dialog by adding some columns to the list view portion which is related to what you are doing.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.