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?
extraModel()
method Q_INVOKABLE to be able to call it from QML. Actualy the property should work too. Did you register youExtraModel
? What error do you get? – folibis