Results 1 to 11 of 11

Thread: Using a QCombobox to select which coloum to display i QListView

  1. #1
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Using a QCombobox to select which coloum to display i QListView

    I have a custom model subclassing QAbstractTableModel. The model contain 4 column of data and in the 5:th column I merge all the data from the other 4 columns.
    I want to display one column at a time in a QListView. To select which column to display I want to use a QComboBox. Is there a way to automatically display the available columns in the QComboBox by using the same model as in the QListView?

    My thoughts:
    • Using a qproxymodel to read the headerdata and return it in data(index). Set the proxy model in the QComboBox
    • Using two separate models

    Any other ideas?

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using a QCombobox to select which coloum to display i QListView

    Does tableView->horizontalHeader()->model() give you a suitable model to use as the list model?

  3. #3
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using a QCombobox to select which coloum to display i QListView

    Maybe. Do you mean that should set the horizontalHeaderModel as the model for the QListView?
    I can set the table model as the model for the QListView and select which column to display with ; setModelColumn(0). But I need to extract the headers in some way to display them in a QComboBox. I think I need to use the same model in both views to be sure that they will be updated whenever data is changed in the model.

  4. #4
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using a QCombobox to select which coloum to display i QListView

    Is there a way to change QListView to use the model's headerData() and coulmnCount() instead of data() and rowCount()?

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using a QCombobox to select which coloum to display i QListView

    You can use proxy model
    Qt Code:
    1. class HeaderProxy : public QAbstractProxyModel
    2. {
    3. public:
    4. explicit HeaderProxy(QObject * parent = 0)
    5. {
    6. ;
    7. }
    8.  
    9. QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
    10. {
    11. if(!parent.isValid())
    12. return createIndex(row, column);
    13. return QModelIndex();
    14. }
    15.  
    16. QModelIndex parent(const QModelIndex & /*child*/) const
    17. {
    18. return QModelIndex();
    19. }
    20.  
    21. QModelIndex mapToSource(const QModelIndex & proxyIndex) const
    22. {
    23. if(!proxyIndex.isValid())
    24. return QModelIndex();
    25.  
    26. return createIndex(proxyIndex.row(), proxyIndex.column());
    27. }
    28.  
    29. QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
    30. {
    31. if(!sourceIndex.isValid())
    32. return QModelIndex();
    33.  
    34. return index(sourceIndex.row(), sourceIndex.column() );
    35. }
    36.  
    37. int rowCount(const QModelIndex &parent) const
    38. {
    39. return sourceModel()->columnCount(parent);
    40. }
    41.  
    42. int columnCount(const QModelIndex &/*parent*/) const
    43. {
    44. return 1;
    45. }
    46.  
    47. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
    48. {
    49. return sourceModel()->headerData(index.row(), Qt::Horizontal, role);
    50. }
    51. };
    52.  
    53. //elsewhere
    54. TableWidget tableWidget;
    55. tableWidget.show();
    56. // Populate rows, columns and other data.
    57.  
    58. QListView listView;
    59. listView.show();
    60.  
    61. QComboBox comboBox;
    62. comboBox.show();
    63.  
    64. HeaderProxy m;
    65. m.setSourceModel(tableWidget.model());
    66. listView.setModel(&m);
    67. comboBox.setModel(&m);
    68.  
    69. //connect the required signal of comboBox to listView
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using a QCombobox to select which coloum to display i QListView

    Thanks!

    It is almost what I want. When I use the proxy model in the combobox the items are not selectable. do I have to implement mapSelectionFromSource and mapSelectionToSource for them to be selectable?

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using a QCombobox to select which coloum to display i QListView

    If you are using the proxy in the example which I posted you should be able to select items in QComboBox.

    BTW selectable option depends on Qt::ItemFlags HeaderProxy::flags(const QModelIndex &index) const; that too if you implement it, else the base class QAbstractProxyModel implementation should enable you to select by default
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #8
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using a QCombobox to select which coloum to display i QListView

    For some reason, the combobox still displays grayed out items.
    I tried to implement
    Qt::ItemFlags flags(const QModelIndex & index) const
    {
    return Qt::ItemIsSelectable;
    }

    but the combobox didn't respond to that.

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using a QCombobox to select which coloum to display i QListView

    1. Don't implement the flags() function
    or
    2. return Qt::ItemSeletable | Qt::ItemEnabaled
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  10. #10
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using a QCombobox to select which coloum to display i QListView

    Thanks
    Adding Qt::ItemIsEnabled did the trick!

  11. #11
    Join Date
    Mar 2013
    Posts
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: Using a QCombobox to select which coloum to display i QListView

    This seems to only work if the row and column count are static and not changing. When Columns are inserted and removed I get empty feilds everywhere.
    What do we need to reimpliment to get the proxy to track the source as it changes size?

    Thanks

Similar Threads

  1. Select item on double clicking in QComboBox
    By tommnaj in forum Newbie
    Replies: 1
    Last Post: 16th February 2012, 10:55
  2. QListView - To select a disabled item
    By agarny in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2011, 01:23
  3. Row and Coloum Position
    By rahulgogoi in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2011, 15:17
  4. Cannot select an item in QComboBox
    By Declan in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2007, 08:08
  5. [QComboBox] (pre-)select no item. (solved)
    By lauranger in forum Qt Programming
    Replies: 4
    Last Post: 17th July 2006, 13:26

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.