0
votes

Recently, I've been interested about how to create a simple general model with submodels and pass its data to .qml page. I got help and did it. But now I've got another problem. Now I can use roles, but I can't use signals or methods.

Here how my code looked like before

basemodel.h

class BaseModel : public QObject
{
    Q_OBJECT
    Q_PROPERTY(ExtraModel* extra READ extraModel CONSTANT)

public:
    explicit BaseModel(QObject *parent = nullptr);

    ExtraModel* extraModel() const { return extraModel_; }

private:
    ExtraModel* extraModel_ = nullptr;
};

basemodel.cpp

BaseModel::BaseModel(QObject *parent)
    : QObject(parent),
      extraModel_(new ExtraModel(this))
{
}

And here how my .qml page was before I changed it to BaseModel

Rectangle {
    signal selectionChanged(int value, string pageTitle, string itemName)
    
    SilicaListView {
        id: list
        anchors.fill: parent
        model: ExtraModel {
            id: _extraModel
            onSelectedChanged: {
                selectionChanged(selected, name, itemName)
            }
        }       
        
        delegate: Rectangle {
            MouseArea {
                anchors.fill: parent
                onClicked: _extraModel.activate(index)
            }
        }
    }
}

And thats how I want it to be (or something like that)

Rectangle {
    signal selectionChanged(int value, string pageTitle, string itemName)
    
    BaseModel {
        id: _baseModel
    }
    
    SilicaListView {
        id: list
        anchors.fill: parent
        model: _baseModel.extra {
            id: _extraModel
            onSelectedChanged: {
                selectionChanged(selected, name, itemName)
            }
        }       
        
        delegate: Rectangle {
            MouseArea {
                anchors.fill: parent
                onClicked: _extraModel.activate(index)
            }
        }
    }
}

But _baseModel.extra doesn't work as component so I asked how to use signals from _baseModel.extra and got the answer: Connections object. So, I searched and found what connections object is. So, I've tried to use it, but I only figured out I can't access my signal from ExtraModel or probably doing something wrong.

That's how I tried to use connection object

SilicaListView {
    id: _list
    anchors.fill: parent

    model: _baseModel.extra

    Connections {
        id: _extraModel
        target: _baseModel.extra
        onSelectedChanged: {
            selectionChanged(selected, name, itemName)
        }
    }
    ...
}

So, the question is how to access my signals and methods from ExtraModel using BaseModel?

1
you can make the extraModel() method Q_INVOKABLE to be able to call it from QML. Actualy the property should work too. Did you register you ExtraModel? What error do you get?folibis
@folibis thanks for your reply. Do you mean make extraModel() method inside BaseModel? Also, by register, did you mean qmlRegisterType? I did, but that's the case for creating BaseModel, do not register other models and just place them through BaseModel.Claire
A Connections object sounds correct. You say you tried it, but you didn't actually show us what you tried.JarMan
@JarMan thanks for you reply. I've attached how I tried to use connections. But I feel like I tried it completely wrong, but I am not sure what exactly wrongClaire

1 Answers

1
votes

To connect to signals using a Connections object, your code should look like this:

        model: _baseModel.extra 

        Connections {
            target: _baseModel.extra
            onSelectedChanged: {
                selectionChanged(selected, name, itemName)
            }
        }  

EDIT: To call invokable functions, you should just be able to do this:

            MouseArea {
                anchors.fill: parent
                onClicked: _baseModel.extra.activate(index)
            }