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. QSortFilterProxyModel - additional column does not alternate row color
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel - additional column does not alternate row color

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.2k 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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phamtv
        wrote on last edited by
        #3

        @
        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);
        

        }
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          [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?

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phamtv
            wrote on last edited by
            #5

            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);
            

            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              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.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phamtv
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rturrentine
                  wrote on last edited by
                  #8

                  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.

                  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