How to use data from the model on the CoverPage

Hi,

I want do display a number on my CoverPage. To get this number, I have a method “rowCount()” that returns an int.

How can I access this number in the QML-file of the CoverPage?

I’ m new to SailfishOS app-development, so sorry if my question sounds really dumb :upside_down_face:

Mmh, is your method rowCount() written in Python?

No, it’s written in normal C++ Code

Have a look at this tutorial. I think you should find your answer. If not, try to search other example on GitHub.

I have already looked at this example and tried to learn from it. However, it doesn’t even use a Cover
and the code from the MainPage

model: DemoModel
{
id: dmodel
}

does not work on the Cover.

Didn’t tested but something like that should work :

main.qml:
ApplicationWindow
{
    initialPage: MyPage {id: myPage}
    cover: MyCoverPage {aNumber: myPage.count}
}

MyPage.qml:
Page
{
  property int count: model.rowCount()
  
  DemoModel {
    id: model
  }
}

CoverPage.qml:
CoverBackground {
  property int aNumber
}

The idea is to expose the value you are interested in from the MyPage object to the CoverPage one.

If you’re working from C++ and exposing values, you can ask for them if you provide the rootContext (or a qml binding) with the object. For instance, in Sailgo:

In the main:

    AbstractBoardController * controller = new AbstractBoardController(model);
...
    view->rootContext()->setContextProperty("boardController",  controller);

In the qml:

    Label {
        id: swart
        property int count: 0
        width: totals.width/4
        text: boardController.blackTotal
}

Given that the AbstractBoardController has a property called blackTotal.

A question. Did you declare the import (qmlRegisterType one) in the cover qml?

import com.example 1.0

Was the example in the example @pherjung mentioned.

No, I didn’t, I only included it in the MainPage

You must include it in the cover page.

I just tested the approach that adds to the root context and that works. There, you don’t have to do anything special in the qml.

Ok, with the root context I won’t have to do something in QML, but I would have to implement some C+±class like the AbstractBoardController in your example, right? Or can I use the model-class directly in setContextProperty() ?

I’d try to go with the model binding that you started with. If you do the import like you did in main in cover, that’s the ‘clean’ way to go. Just add the declaration and it should work …

And you can use the model class directly through the setContext route, but it’s not the ‘cleanest’ way to go.

Thank you, with the import and something I accidently discovered, it works now!!

Yeah! There’s a lot of good sailfish code around (I belong to the less good code quality variety :slight_smile: I go to monich (Slava Monich) · GitHub project for inspiration quite often but there’s lots of stuff:
sailfish-os-apps · GitHub has over 200 projects listed.

1 Like

This is close but not quite right when the count changes the count property declared in the page doesn’t update correctly. Surely if the count doesn’t change during app life-cycle then example is fine. However, if it does change during app life-cycle then following changes would be needed

  • Define readonly property to the C++ header: Q_PROPERTY(int count READ count NOTIFY countChanged)
  • Add public signal void countChanged();
  • Whenever you insert or remove items from the model emit countChanged();
  • Property binding changes to property int count: model.count

Worth noting that if the same model is used by many pages / views you can declare the model itself inside the main.qml, something like this:

main.qml:
ApplicationWindow
{
    initialPage: MyPage {
        aNumber: model.count
    }
    cover: MyCoverPage {
        aNumber: mount.count
    }

    DemoModel {
        id: model
    }
}

This would render the intermediate count property on the page unnecessary.

1 Like

You’re absolutely right. Thanks, I forgot about the fact that count should be a property and not the result of function if one wants to be dynamic.

@dcaliste no worries! I know that you know :ok_hand: ! Thank you for your example and guidance. It was really good example and made easy to add additions on top your example. Cheers :pray: and I hope this helps @Schniki12.

Let’s keep on helping each other! I’ll try to add my ideas/guidances more for you all. Don’t hesitate to to CC me to your questions.

1 Like

Since the last time I wrote here, my application has evolved and now I need that number to change during runtime. So I tried @rainemak 's approach with the property. The value does correctly show up on the cover. I have also added the emit-Signal code. But when I now want to catch that signal with “onCountChanged”, nothing happens. Am I missing something here?