1

i am trying to set applicationwindow {} size for my android apk, so i wish to read the values from cpp file:

main.cpp:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QScreen *screen = QApplication::screens().at(0);
    QVariant sz_width = screen->availableSize().width();
    QVariant sz_height = screen->availableSize().height();

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

then from qml file to read it (main.qml):

ApplicationWindow {
    id: mainWindow
    visible: true
    width: sz_width
    height: sz_height 
}

this is in order to easly manipulate with all object sizes later on in qml, so basicaly for example i use font size by mainWindow*0.5 so i can have proper font size for every app resolution, but it only works if i realy set up the variables width and height...

Maybe this solution is a but "morbid", but however I would like to do it this way if you can help me with proper syntax...

Thank you

2
  • If you use QML, don't do GUI in C++, it is not necessary and an anti-pattern. From QML you can simply set visibility: Window.FullScreen Commented Feb 10, 2019 at 16:15
  • You don't provide all the code ie. you don't actually set the values in main.cpp to be visible in QML. Commented Feb 10, 2019 at 16:45

1 Answer 1

6

To quickly make C++ values visible in QML you can set them as a context property:

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("screenWidth", sz_width);
engine.rootContext()->setContextProperty("screenHeight", sz_height);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

After this the variables are available in QML by the quoted names ("screenWidth" and "screenHeight") (these could also match the C++ variable name if you prefer).

QSize type is also recognized by QML, so you could just set the size as one variable.

engine.rootContext()->setContextProperty("screenSize", screen->availableSize());

Also in this case the information you're looking for is already available in QML... check the Screen attached object and also the Qt.application.screens object/property for a list of available screens.

ADDED:

Since the linked documentation doesn't mention it directly, it should be noted that context property variables set in this way have no change notifier signals. Therefore they do not automatically update in QML, unlike other "bindable" properties. The only way to get QML to update the value automatically is to set the context property again (or create some signal which QML can connect to and cause it to re-read the value).

I can't find where exactly this is mentioned in the Qt docs, but the QQmlContext page provides a (subtle) clue:

The context properties are defined and updated by calling QQmlContext::setContextProperty().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.