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 and strange interaction with hideColumn of a QTableView
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel and strange interaction with hideColumn of a QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 195 Views
  • 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.
  • IlaroI Offline
    IlaroI Offline
    Ilaro
    wrote on last edited by
    #1

    Hello all, I am experiencing a really weird behaviour in my application (Qt 5.15.1 - Widgets).

    I have a QSqlRelationalTableModel that maps to a table in an sqlite database. This model is passed to a QSortFilterProxyModel for simple filtering and sorting, I apply the filter with setFilterRegularExpression. The relevant code is this:

    // Model for the slits
        m_model_slits = new SlitsQSqlRelationalTableModel( this );
        
        m_model_slits->setTable( "slits" );
        m_model_slits->setEditStrategy( QSqlRelationalTableModel::OnManualSubmit );
        m_model_slits->setRelation( m_model_slits->fieldIndex( "lot_id" ), QSqlRelation( "lots", "id", "id" ) );
        m_model_slits->select();
        
        m_proxy_slits = new QSortFilterProxyModel( this );
        
        m_proxy_slits->setSourceModel( m_model_slits );
        m_proxy_slits->sort( m_model_slits->fieldIndex( "serial" ) );
        m_proxy_slits->setFilterKeyColumn( m_model_slits->fieldIndex( "lots_id_2" ) );
        
        ui->wizard_serial_list->setModel( m_proxy_slits );
        ui->wizard_serial_list->setItemDelegateForColumn( m_model_slits->fieldIndex( "outcome" ), new OutcomeItemDelegate( this ) );
        // TODO: for some reason, hiding the id column will break the filtering. Should be investigated further
    //    ui->wizard_serial_list->hideColumn( m_model_slits->fieldIndex( "id" ) );
        ui->wizard_serial_list->hideColumn( m_model_slits->fieldIndex( "lots_id_2" ) );
    

    SlitsQSqlRelationalTableModel is a really simple modification to disable editing in most columns:

    class SlitsQSqlRelationalTableModel : public QSqlRelationalTableModel
    {
        Q_OBJECT
        
    public:
        explicit SlitsQSqlRelationalTableModel( QObject * p_parent = nullptr, const QSqlDatabase & p_database = QSqlDatabase() );
        
    private:
        Qt::ItemFlags flags( const QModelIndex & p_index ) const override;
    };
    
    SlitsQSqlRelationalTableModel::SlitsQSqlRelationalTableModel( QObject * p_parent, const QSqlDatabase & p_database )
        : QSqlRelationalTableModel( p_parent, p_database )
    {}
    
    
    Qt::ItemFlags SlitsQSqlRelationalTableModel::flags( const QModelIndex & p_index ) const
    {
        // Get original flags
        Qt::ItemFlags flags = QSqlTableModel::flags( p_index );
        
        // Ensure no edit flags on column that should be read only
        if ( p_index.column() != fieldIndex( "notes" ) )
        {
            flags &= ~Qt::ItemIsEditable;
        }
        
        // Return flags
        return flags;
    }
    

    Now, in the table view I don't like to see the ID column because it's irrelevant to the operator. But if I hide it, i get only partials results from the model! If I keep it shown, than I get all results. It does not make any sense to me.
    So: leaving the filter absolutely unchanged, I got different results showing or hiding the ID column from the view.

    Any idea?
    Thanks a lot.

    Ilario

    JonBJ 1 Reply Last reply
    0
    • IlaroI Ilaro

      Hello all, I am experiencing a really weird behaviour in my application (Qt 5.15.1 - Widgets).

      I have a QSqlRelationalTableModel that maps to a table in an sqlite database. This model is passed to a QSortFilterProxyModel for simple filtering and sorting, I apply the filter with setFilterRegularExpression. The relevant code is this:

      // Model for the slits
          m_model_slits = new SlitsQSqlRelationalTableModel( this );
          
          m_model_slits->setTable( "slits" );
          m_model_slits->setEditStrategy( QSqlRelationalTableModel::OnManualSubmit );
          m_model_slits->setRelation( m_model_slits->fieldIndex( "lot_id" ), QSqlRelation( "lots", "id", "id" ) );
          m_model_slits->select();
          
          m_proxy_slits = new QSortFilterProxyModel( this );
          
          m_proxy_slits->setSourceModel( m_model_slits );
          m_proxy_slits->sort( m_model_slits->fieldIndex( "serial" ) );
          m_proxy_slits->setFilterKeyColumn( m_model_slits->fieldIndex( "lots_id_2" ) );
          
          ui->wizard_serial_list->setModel( m_proxy_slits );
          ui->wizard_serial_list->setItemDelegateForColumn( m_model_slits->fieldIndex( "outcome" ), new OutcomeItemDelegate( this ) );
          // TODO: for some reason, hiding the id column will break the filtering. Should be investigated further
      //    ui->wizard_serial_list->hideColumn( m_model_slits->fieldIndex( "id" ) );
          ui->wizard_serial_list->hideColumn( m_model_slits->fieldIndex( "lots_id_2" ) );
      

      SlitsQSqlRelationalTableModel is a really simple modification to disable editing in most columns:

      class SlitsQSqlRelationalTableModel : public QSqlRelationalTableModel
      {
          Q_OBJECT
          
      public:
          explicit SlitsQSqlRelationalTableModel( QObject * p_parent = nullptr, const QSqlDatabase & p_database = QSqlDatabase() );
          
      private:
          Qt::ItemFlags flags( const QModelIndex & p_index ) const override;
      };
      
      SlitsQSqlRelationalTableModel::SlitsQSqlRelationalTableModel( QObject * p_parent, const QSqlDatabase & p_database )
          : QSqlRelationalTableModel( p_parent, p_database )
      {}
      
      
      Qt::ItemFlags SlitsQSqlRelationalTableModel::flags( const QModelIndex & p_index ) const
      {
          // Get original flags
          Qt::ItemFlags flags = QSqlTableModel::flags( p_index );
          
          // Ensure no edit flags on column that should be read only
          if ( p_index.column() != fieldIndex( "notes" ) )
          {
              flags &= ~Qt::ItemIsEditable;
          }
          
          // Return flags
          return flags;
      }
      

      Now, in the table view I don't like to see the ID column because it's irrelevant to the operator. But if I hide it, i get only partials results from the model! If I keep it shown, than I get all results. It does not make any sense to me.
      So: leaving the filter absolutely unchanged, I got different results showing or hiding the ID column from the view.

      Any idea?
      Thanks a lot.

      Ilario

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Ilaro said in QSortFilterProxyModel and strange interaction with hideColumn of a QTableView:

      Any idea?

      My guess: I think you are talking about hiding the column which is the one mapped for foreign key in the relation, is that right? If so, maybe there is an issue about that? If you hide a column which has nothing to do with the relation does it work better?

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        hiding the id column will break the filtering

        I struggle to see how given the proxy and the view live completely independent lives (i.e. when the proxy filters it doesn't know if the column is hidden or not)

        Could you share the code that you use to apply the filter?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        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