Results 1 to 3 of 3

Thread: How to display selected columns in QTableView widget.

  1. #1
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to display selected columns in QTableView widget.

    hi,

    How can i display selected columns from a Model into my QTableView widget.
    Say my model has 5 cloumns and i want o display two in a QTableView widget and
    populate any other combo box widget with data from the third colunm of the Model.

    Qt Code:
    1. int MyModel::columnCount(const QModelIndex& p) const
    2. {
    3. if ( p.isValid() || !sourceModel() )
    4. return 0;
    5.  
    6. return 3;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Then three columns are shown in my QTableView widget but i want only two and data for the third to be put in a comboBox.

    help!!!!

    Regards,
    GK

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to display selected columns in QTableView widget.

    maybe this method will help
    Qt Code:
    1. QModelIndexList QItemSelectionModel::selectedColumns ( int row = 0 ) const
    To copy to clipboard, switch view to plain text mode 
    http://doc.trolltech.com/4.4/qitemselectionmodel.html#selectedColumns

  3. #3
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to display selected columns in QTableView widget.

    If you have a model with 5 columns and you only want to view 3 in your view, you have to add a subclass of QSortFilterProxyModel(or abstract if you prefer creating some of the functions yourself) between them.

    On the subclass you create you need to implement:
    Qt Code:
    1. bool MySortFilterProxyModel::filterAcceptsColumns(int source_column,
    2. const QModelIndex &source_parent) const
    3. {
    4. //return true for columns you want, else return false...
    5. }
    To copy to clipboard, switch view to plain text mode 

    See documentation on QSortFilterProxyModel for example on filtering rows. it's the same for columns.

    Then use the proxymodel on the view you have.

    When accessing data through a selection model on the view you need to use the mapToSource and mapFromSource on the selectionmodel to find the corresponding item in your model. You can also use the QSortFilterProxyModel for sorting and filtering... which is something most applications want to have...

    cheers,
    Leif

Similar Threads

  1. Replies: 4
    Last Post: 10th March 2007, 19:01
  2. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 13:07
  3. Replies: 3
    Last Post: 12th April 2006, 09:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.