Results 1 to 13 of 13

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

  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.

  12. #12
    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

    I do (in c++) But your code really looks fine apart from ignoring the parent indexes.

    Here is a complete working example:
    Qt Code:
    1. #include <QAbstractTableModel>
    2. #include <QSortFilterProxyModel>
    3. #include <QTableView>
    4. #include <QApplication>
    5.  
    6.  
    7. class Model : public QAbstractTableModel {
    8. public:
    9. Model() : QAbstractTableModel(){
    10. for(int i=0;i<3;i++){
    11. QVector<int> row;
    12. for(int j=0;j<3;j++)
    13. row << qrand()%100;
    14. m_data << row;
    15. }
    16. }
    17. int rowCount(const QModelIndex &parent = QModelIndex()) const {
    18. return m_data.size();
    19. }
    20. int columnCount(const QModelIndex &parent = QModelIndex()) const {
    21. return m_data.at(0).size();
    22. }
    23. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
    24. if(!index.isValid()) return QVariant();
    25. else if(role!=Qt::DisplayRole) return QVariant();
    26. return m_data[index.row()][index.column()];
    27. }
    28. private:
    29. QVector< QVector<int> > m_data;
    30. };
    31.  
    32. int main(int argc, char **argv){
    33. QApplication app(argc, argv);
    34. Model model;
    35. proxy.setSourceModel(&model);
    36. tv.setModel(&proxy);
    37. tv.show();
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    Check if the problem is not outside the model class (i.e. maybe the proxy model gets deleted somewhere inbetween setting it on the view and launching the application).
    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.


  13. The following user says thank you to wysota for this useful post:

    draGy (5th July 2009)

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

    Default

    Hi,

    thanks for that little example.
    I converted it in python and it works. I will not try to find out whats wrong with my programm with the help of that code.
    When I find the solution I will post it here.
    Thanks again.

    Here they python code:
    Qt Code:
    1. import sys
    2. from PyQt4.QtCore import *
    3. from PyQt4.QtGui import *
    4.  
    5. class Model(QAbstractTableModel):
    6. def __init__(self, parent=None):
    7. QAbstractTableModel.__init__(self, parent)
    8. self.m_data = [["1","2","3"],["4","5","6"],["7","8","9"]]
    9.  
    10. def rowCount(self, parent=QModelIndex()):
    11. return len(self.m_data)
    12.  
    13. def columnCount(self, parent=QModelIndex()):
    14. return len(self.m_data[0])
    15.  
    16. def data(self, index, role = Qt.DisplayRole):
    17. if(not index.isValid()):
    18. return QVariant()
    19. elif(role!=Qt.DisplayRole):
    20. return QVariant()
    21. return QVariant(self.m_data[index.row()][index.column()])
    22.  
    23. if __name__ == "__main__":
    24. app = QApplication(sys.argv);
    25. model = Model()
    26. proxy.setSourceModel(model)
    27. tv = QTableView()
    28. tv.setModel(proxy);
    29. tv.show();
    30. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Oh man, you were so right.

    I wasn't the fault of the model.
    I thought once I set
    Qt Code:
    1. proxyModel.setSourceModel(myModel)
    To copy to clipboard, switch view to plain text mode 
    that myModel would be referenced by proxyModel and thus stay alive. But apparently it got deleted after the method exited and thus I never saw something.

    I now saved it as an instance variable (instead of an local variable) and now it works.


    Man, the whole day got wasted, just because of this.

    Well anyways, thanks a lot for your help!
    Last edited by wysota; 5th July 2009 at 16:35.

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.