QSortFilterProxyModel not working with a QAbstractListModel derived model.



  • @ NewModel model;
    QAbstractItemModel * pm = qobject_cast<QAbstractItemModel *>(&model);
    QSortFilterProxyModel proxy;
    proxy.setSourceModel(pm);
    proxy.setSortRole(NewModel::WordRole);
    proxy.setDynamicSortFilter(true);@

    This is what I tried, but it doesn't seem to work. The ListView in QML is able to pick up the model through the proxy, but there is no sorting. Am I doing something wrong?



  • proxy has allocated on stack. dumb mistake.



  • [quote author="Konstantin Ritt" date="1367214584"]proxy has allocated on stack. dumb mistake.[/quote]

    Care to elaborate what is so dumb about it and what does it have to do with the sorting proxy not working? Or was that just a random "dumb" statement?


  • Lifetime Qt Champion

    You allocate your proxy on the stack, so it will be destroyed at the end of the function hence no sorting. You have to allocate it on the heap to keep it and get your sorting.

    I think Konstantin was just saying the it was a simple mistake that we all do/did from time to time.



  • The stack is also the one which instantiates the application so everything allocated there lives for the duration of the application. If Konstantin wasn't so hasty to plant his insult he'd figure it out. And what is worse, absent any information related to the subject, the essence of his post boils down to the contained insult. Oh, let's not forget the abysmal grammar too...


  • Lifetime Qt Champion

    I have to disagree i.e.

    @
    int main(int argc, char *argv[]
    {
    QApplication app(argc, argv);
    QWidget w;
    {
    NewModel model;
    QAbstractItemModel * pm = qobject_cast<QAbstractItemModel *>(&model);
    QSortFilterProxyModel proxy;
    proxy.setSourceModel(pm);
    proxy.setSortRole(NewModel::WordRole);
    proxy.setDynamicSortFilter(true);
    }
    // both model and proxy have now been destroyed because they are out of scope
    w.show()
    return app.exec();
    }@



  • About 2 seconds of thought should be enough to determine that failure to sort could not possibly be the result of trying to use a deleted local object. I'd expect to get a serious runtime error if I try to use a non-existent object as a model, don't you think?

    Your solidarity is heartwarming but in this particular case misplaced. There is absolutely nothing to hint that the sorting proxy fails to sort because of a dangling reference, nor any possible way to even reference an entirely local object outside of its scope short of an external handle to reach it. I honestly see no point in you defending a hasty "dumb" post absent thought or helpfulness.



  • About 2 seconds of thought is enough to imagine few more "dumb" mistakes in the code you didn't show: missing Q_OBJECT macro, not deriving from QAIM, not calling sort() on a column of interest, not supporting NewModel::WordRole role in the model, etc.

    Check if sorting doesn't work to you with some proven model, i.e. QStandardItemModel: if it doesn't, see what's wrong with the view.



  • bq. About 2 seconds of thought is enough to imagine few more “dumb” mistakes in the code you didn’t show: missing Q_OBJECT macro, not deriving from QAIM, not calling sort() on a column of interest, not supporting NewModel::WordRole role in the model, etc.

    Konstantin - I see you are firm on the course of making premature statements absent thought:

    • Q_OBJECT is not missing
    • I need a list model, thus I implement a QAbstractListModel
    • there is but a single column in the model
    • WordRole is supported in the model

    There is no need to participate if you have nothing of value to contribute with, I am sure the internet offers plenty of other places for you to troll around...



  • Sorry, I didn't realize this is a "here are 6 meaningless lines of code, guess what's wrong besides them" game.

    Check if sorting doesn’t work to you with some proven model, i.e. QStandardItemModel: if it doesn’t, see what’s wrong with the view.

    Still valuable ^



  • bq. Sorry, I didn’t realize this is a “here are 6 meaningless lines of code, guess what’s wrong besides them” game.

    Obviously, you were playing a different game right from the start, the game of premature offensive random statements. It is funny and ironic that you above all speak of meaningless lines... Sorry to cut your poopscapade short, but I doubt further input will be much more beneficial than what you "contributed" so far. Thanks a lot, couldn't have done it without you ;)



  • I had the same problem, but it was because I wasn't calling sort on the proxy. I have come across a problem which I don't understand and one of you might.

    I have a qml file that worked fine when using a QAbstractListModel, but when I apply a QSortFilterProxyModel to it the qml spits out ReferenceError, documentViewer is not defined.

    @
    Rectangle {
    id: documentViewer
    color: "#E5E5E5"
    clip: true
    visible: false
    focus:true
    border.width: 0

    property bool isInSelectionMode: false
    
    property int thumbNailWidth: 152
    property int thumbNailHeight: 152
    
    property int thumbNailTopMargin: 5
    property int thumbNailBottomMargin: 45
    property int thumbNailLeftMargin: 22
    property int thumbNailRightMargin:  22
    
    Component {
        id: modelDelegate
    
        Rectangle {
            id: imageRect
            width: thumbNailLeftMargin + thumbNailRightMargin + thumbNailWidth
            height: thumbNailTopMargin + thumbNailBottomMargin + thumbNailHeight
            color: "transparent"
            border.color: "transparent"
    
            Item {
                id: thumbNail
                x: thumbNailLeftMargin
                y: thumbNailTopMargin
                width: documentViewer.thumbNailWidth
                height: documentViewer.thumbNailHeight
    
                Image {
                    id: thumbNailImage
                    anchors.fill: parent
                    clip: true
                    source: imageSource
                    sourceSize.width:  documentViewer.thumbNailWidth
                    sourceSize.height: documentViewer.thumbNailHeight
                }
    
                MouseArea {
                    id: mouseArea
                    anchors.fill: parent
    
                    onPressAndHold: {
                        if(isItemSelectable && !documentViewer.isInSelectionMode)
                        {
                            isItemSelected = !isItemSelected;
                            documentViewer.isInSelectionMode = !documentViewer.isInSelectionMode;
                        }
                    }
    
                    onPressed: {
                        if(documentViewer.isInSelectionMode && isItemSelectable)
                        {
                            isItemSelected = !isItemSelected;
                            mouse.accepted = true;
                        }
                    }
    
                    onClicked: {
                        if(documentIndex == 0 && !documentViewer.isInSelectionMode)
                        {
                            DocumentManager.NewDocument();
                            documentViewer.done(1);
                        }
                        else if(documentIndex != 0 && !documentViewer.isInSelectionMode)
                        {
                            DocumentManager.OpenDocument(name);
                            documentViewer.done(1);
                        }
                    }
                }
            }
       }
    

    }

    @

    I have a list view that used this delegate, but I haven't showed it in this code snippet. I don't get why when I apply the QALM on the ListView it has no problem determining what documentViewer is, but when I apply QSFPM it gives me a reference error. Any ideas?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.