Results 1 to 6 of 6

Thread: Blank QListView when using QSortFilterProxyModel

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Blank QListView when using QSortFilterProxyModel

    Hi, I'm playing with QSortFilterProxyModel (never used before); so I created a sample application with a QListView whose items content is controlled by delegates. This is a piece of code without proxy:

    Qt Code:
    1. model = new MyModel(this);
    2.  
    3. ItemDelegate *itemDelegate = new ItemDelegate(ui->listView);
    4. ui->listView->setItemDelegate(itemDelegate);
    5.  
    6. ui->listView->setModel(model);
    7.  
    8. setModelData_1();
    To copy to clipboard, switch view to plain text mode 

    It works fine, but if I use MySortFilterProxyModel, like in this snippet:

    Qt Code:
    1. model = new MyModel(this);
    2. proxyModel = new MySortFilterProxyModel(this);
    3.  
    4. ItemDelegate *itemDelegate = new ItemDelegate(ui->listView);
    5. ui->listView->setItemDelegate(itemDelegate);
    6.  
    7. ui->listView->setModel(proxyModel);
    8. proxyModel->setSourceModel(model);
    9.  
    10. setModelData_1();
    To copy to clipboard, switch view to plain text mode 

    the view is empty.

    MySortFilterProxyModel is very minimal; it contains only bool QSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const returning a true.

    Where am I wrong?

    Thanks for your help.
    Giuseppe CalÃ

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

    Default Re: Blank QListView when using QSortFilterProxyModel

    set the source of proxy model first
    Qt Code:
    1. proxyModel->setSourceModel(model);
    2. ui->listView->setModel(proxyModel);
    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.

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Blank QListView when using QSortFilterProxyModel

    Quote Originally Posted by Santosh Reddy View Post
    set the source of proxy model first
    Qt Code:
    1. proxyModel->setSourceModel(model);
    2. ui->listView->setModel(proxyModel);
    To copy to clipboard, switch view to plain text mode 
    swapping lines does not help.

    Perhaps I catched the problem; proxyModel->setSourceModel(...) must be called after feeding model with data. But now my program crashes at the following line:

    Qt Code:
    1. void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return;
    5.  
    6. const MyModel *model = qobject_cast<const MyModel*>(index.model());
    7.  
    8. ...
    9. QString first = model->data(index, ItemRoles::First).toString(); // <-- debugger stops here
    10.  
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 

    The debugger says that model's value is 0x0 and first's value is <not accessible>
    Giuseppe CalÃ

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

    Default Re: Blank QListView when using QSortFilterProxyModel

    You should be doing this.

    Qt Code:
    1. void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return;
    5.  
    6. const MyModel *model = qobject_cast<const MyModel*>(index.model());
    7.  
    8. ...
    9. if(model != 0)
    10. QString first = model->data(index, ItemRoles::First).toString(); // <-- debugger stops here
    11.  
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 


    BTW, I don't see a need to get the model, if you need data of the index, you could use
    Qt Code:
    1. QVariant QModelIndex::data ( int role = Qt::DisplayRole ) const
    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.

  5. The following user says thank you to Santosh Reddy for this useful post:

    jiveaxe (26th March 2013)

  6. #5
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Blank QListView when using QSortFilterProxyModel

    AFAIK, index.model () here returns proxy model and not underlying MyModel.
    Anyway why do you need cast to MyModel if you're using model->data()?

  7. #6
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Blank QListView when using QSortFilterProxyModel

    Quote Originally Posted by Santosh Reddy View Post
    BTW, I don't see a need to get the model, if you need data of the index, you could use
    Qt Code:
    1. QVariant QModelIndex::data ( int role = Qt::DisplayRole ) const
    To copy to clipboard, switch view to plain text mode 
    Very very thanks. Used your line and problem is fixed.
    Giuseppe CalÃ

Similar Threads

  1. How can I add blank space to a QTableView?
    By Paladin12 in forum Qt Programming
    Replies: 0
    Last Post: 15th August 2011, 11:39
  2. QDialog is blank
    By daviddoria in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2011, 04:10
  3. Add a blank entry in a combobox
    By lynnH in forum Qt Programming
    Replies: 9
    Last Post: 21st April 2011, 10:40
  4. issue about blank area appear
    By cherrydou in forum Qt Programming
    Replies: 5
    Last Post: 23rd May 2008, 07:34

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
  •  
Qt is a trademark of The Qt Company.