Results 1 to 13 of 13

Thread: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    Hi,

    I want to use the QSortFilterProxyModel on my custom model for sorting/filtering purpose. But when applying it, the list in my TableView is empty.

    If I do not use my custom model, but the QStandardItemModel (as in the examples "basic sort/filter model"), it works.

    So it seems my custom model is at fault here, although it works just fine if I do not use the QSortFilterProxyModel.
    Does my custom model require additional method implementations for it to work?
    Or do I have subclass the QSortFilterProxyModel? If so, what would I need to change?

    Here is my custom model:
    Qt Code:
    1. class MyTableModel(QAbstractTableModel):
    2. def __init__(self, datain, parent=None, *args):
    3. QAbstractTableModel.__init__(self, parent, *args)
    4. self.arraydata = datain
    5.  
    6. def rowCount(self, parent):
    7. return len(self.arraydata)
    8.  
    9. def columnCount(self, parent):
    10. return len(self.arraydata[0])
    11.  
    12. def data(self, index, role):
    13. if not index.isValid():
    14. return QVariant()
    15. elif role != Qt.DisplayRole:
    16. return QVariant()
    17. return QVariant(self.arraydata[index.row()][index.column()])
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    How do you add data to your model? Do you remember about emitting signals?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    In this simple programm I have an array
    Qt Code:
    1. my_array = [['00','01','02'],
    2. ['10','11','12'],
    3. ['20','21','22']]
    To copy to clipboard, switch view to plain text mode 
    which will be given to the constructor of the model.
    Qt Code:
    1. tablemodel = MyTableModel(my_array, self)
    To copy to clipboard, switch view to plain text mode 

    At this stage I simply want to display my model. Later on I will add the necessary methods where I can add items to the model. Or do I need these methods already for the sorting/filtering?

    As for "emmiting signals" I am not sure what you are getting at. I know the idea behind the signal/slot thing, but I don't see where I need it in my model or view.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    Can you show us the code where you use the sort filter proxy model?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    here it is:
    Qt Code:
    1. myView = QTableView()
    2. myModel = MyTableModel(my_array)
    3. proxyModel = QSortFilterProxyModel()
    4. proxyModel.setSourceModel(myModel)
    5. #proxyModel.setDynamicSortFilter(True)
    6. myView.setModel(proxyModel)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    The code looks fine, could you check what does columnCount() return for both the source model and the proxy?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    I added the print statement, so I can see what will be returned.
    Qt Code:
    1. def columnCount(self, parent):
    2. print(len(self.arraydata[0]))
    3. return len(self.arraydata[0])
    To copy to clipboard, switch view to plain text mode 

    as well for
    Qt Code:
    1. myView.setModel(proxyModel)
    To copy to clipboard, switch view to plain text mode 
    as for
    Qt Code:
    1. myView.setModel(myModel)
    To copy to clipboard, switch view to plain text mode 

    it always returns 3

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    It's not what I mean. Call columnCount() on both the proxy and the source model and print the result. It is important to see what the proxy reports (0 or 3).

    By the way, if you omit the proxy then the view displays the data properly, correct?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    Qt Code:
    1. print("proxyModel:")
    2. print(proxyModel.columnCount(QModelIndex()))
    3. print("myModel:")
    4. print(myModel.columnCount(QModelIndex()))
    To copy to clipboard, switch view to plain text mode 

    results in:
    Qt Code:
    1. proxyModel:
    2. 3
    3. myModel:
    4. 3
    To copy to clipboard, switch view to plain text mode 


    By the way, if you omit the proxy then the view displays the data properly, correct?
    yes. It works fine without the proxy model.

    Ps: I will be out for a few hours (so my next reply won't be coming right away ).
    And before I forget it: thanks for the fast replies.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    The only thing that comes to my mind is that you didn't implement the parent() method although it might already be implemented for the table model. On the other hand it wouldn't hurt to return an empty model index from it. Also make sure you return non-zero values of rowCount and columnCount only if the parent index is invalid.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model

    Would anyone have an example where QSortFilterProxyModel is used with a custom model?
    Preferably for PyQt.

Similar Threads

  1. custom widget and model
    By asieriko in forum Qt Programming
    Replies: 2
    Last Post: 6th October 2008, 14:11
  2. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  3. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  4. Help with getting my custom model working
    By thomaspu in forum Qt Programming
    Replies: 19
    Last Post: 29th July 2007, 18:35
  5. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54

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.