[Interest] How to read screen information from QGuiApplication to put in qputenv environment variable?

Jérôme Godbout godboutj at amotus.ca
Mon Feb 15 14:56:48 CET 2021

Since you seem to be using Qml, you can take a look at https://doc.qt.io/qt-5/qml-qtquick-window-screen.html that could help you and have most of the job done, you could do your own qml singleton if you need to compute scale base on those value. You could access those property fom C++ and export you env vairbale if you really need to, but I would stick to View = Qml, keep thing in Qml as much as possible, so your view stay into the view layer.

Jérôme Godbout
Software / Firmware Lead

C: (581) 777-0050
O: (418) 800-1073 ext.: 109
godbotuj at amotus.ca<mailto:godbotuj at amotus.ca>
[cid:image001.png at 01D70378.7E27B7D0]
dimonoff.com<https://www.dimonoff.com/>   |    amotus.ca<https://www.amotus-solutions.com/>

From: Interest <interest-bounces at qt-project.org> On Behalf Of Anisha kaul
Sent: February 15, 2021 6:51 AM
To: interest at qt-project.org
Subject: [Interest] How to read screen information from QGuiApplication to put in qputenv environment variable?


The problem is that qputenv does not work if written after the initialization of QGuiApplication.

I want to calculate scale according to the information from QGuiApplication.

How do I do it? The following code throws a segmentation fault.



#include <QGuiApplication>

#include <QQmlApplicationEngine>

#include <QScreen>

#include <QDebug>

int main(int argc, char *argv[])


    qreal abc = QGuiApplication::primaryScreen()->physicalDotsPerInch();

    qDebug() << "null " << abc;

    qputenv("QT_SCALE_FACTOR", "2");

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    const QUrl url(QStringLiteral("qrc:/main.qml"));

    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,

                     &app, [url](QObject *obj, const QUrl &objUrl) {

        if (!obj && url == objUrl)


    }, Qt::QueuedConnection);


    return app.exec();

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210215/4c1a6a72/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 7814 bytes
Desc: image001.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210215/4c1a6a72/attachment.png>

More information about the Interest mailing list