Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

c++ - How to approach development of new Qt 5.7+ High-DPI Per Monitor DPI Aware application?

I have read the official Qt documentation and many articles and questions on StackOverflow about high DPI support in Qt. All of them focus on porting old applications and making them work with as least changes as possible.

But if I were to start a brand new application, with the intention to support per-monitor DPI aware app, what is the best approach?

If I understand correctly, Qt::AA_EnableHighDpiScaling is the very opposite of what I want. I should actually disable HighDpiScaling and calculate all the dimensions manually on runtime?

Many of the suggestions say not to use sizes at all, to use floating layouts. But in many cases at least minimum width and/or minimum height is desired to be present. As Qt Designer only allows me to put values in absolute pixels, what is the right approach? Where should I place the code to recalculate dimensions if monitor resolution changes?

Or should I just go with the automatic scaling?

My solution from previous Qt app (not well tested)

In one of my older apps where I tried to add HighDPI support, I used this approach - list all children of DOM and resize them one by one given some ratio. Ratio = 1 would produce dimensions equal to the ones I specified in Qt Designer.

    void resizeWidgets(MyApp & qw, qreal mratio)
    {

        // ratio to calculate correct sizing
        qreal mratio_bak = mratio;

        if(MyApp::m_ratio != 0)
            mratio /= MyApp::m_ratio;

        // this all was done so that if its called 2 times with ratio = 2, total is not 4 but still just 2 (ratio is absolute)
        MyApp::m_ratio = mratio_bak;

        QLayout * ql = qw.layout();

        if (ql == NULL)
            return;

        QWidget * pw = ql->parentWidget();

        if (pw == NULL)
            return;

        QList<QLayout *> layouts;

        foreach(QWidget *w, pw->findChildren<QWidget*>())
        {
            QRect g = w->geometry();

            w->setMinimumSize(w->minimumWidth() * mratio, w->minimumHeight() * mratio);
            w->setMaximumSize(w->maximumWidth() * mratio, w->maximumHeight() * mratio);

            w->resize(w->width() * mratio, w->height() * mratio);
            w->move(QPoint(g.x() * mratio, g.y() * mratio));
            
        }

        foreach(QLayout *l, pw->findChildren<QLayout*>())
        {
            if(l != NULL && !(l->objectName().isEmpty()))
                layouts.append(l);
        }
        
        foreach(QLayout *l, layouts) {
            QMargins m = l->contentsMargins();

            m.setBottom(m.bottom() * mratio);
            m.setTop(m.top() * mratio);
            m.setLeft(m.left() * mratio);
            m.setRight(m.right() * mratio);

            l->setContentsMargins(m);

            l->setSpacing(l->spacing() * mratio);

            if (l->inherits("QGridLayout")) {
                QGridLayout* gl = ((QGridLayout*)l);

                gl->setHorizontalSpacing(gl->horizontalSpacing() * mratio);
                gl->setVerticalSpacing(gl->verticalSpacing() * mratio);
            }

        }
        
        QMargins m = qw.contentsMargins();

        m.setBottom(m.bottom() * mratio);
        m.setTop(m.top() * mratio);
        m.setLeft(m.left() * mratio);
        m.setRight(m.right() * mratio);

        // resize accordingly main window
        qw.resize(qw.width() * mratio, qw.height() * mratio);
        qw.setContentsMargins(m);
        qw.adjustSize();
    }

which is called from main:

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

    QApplication a(argc, argv);
    MyApp w;

    // gets DPI
    qreal dpi = a.primaryScreen()->logicalDotsPerInch();

    MyApp::resizeWidgets(w, dpi / MyApp::refDpi);

    w.show();

    return a.exec();
}

I don't consider this a good solution. Given that I am starting fresh and I can fully customise my code to the latest Qt standards, what approach should I use to get HighDPI apps?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I were to start a brand new application, with the intention to support per-monitor DPI awareness, what is the best approach?

We don't rely on Qt for automatic scaling in per-monitor DPI aware mode. At least Qt 5.7-based app with Qt::AA_EnableHighDpiScaling set does not do that and 'high DPI scaling' is more of accurate drawing regardless of pixel density.

And to invoke per-monitor DPI aware mode you need to modify Qt.conf file at the same directory where you project executable file is:

[Platforms]
# 1 - for System DPI Aware
# 2 - for Per Monitor DPI Aware
WindowsArguments = dpiawareness=2

# May need to define this section as well
#[Paths]
#Prefix=.

If I understand correctly, Qt::AA_EnableHighDpiScaling is the very opposite of what I want. I should actually disable HighDpiScaling and calculate all the dimensions manually on runtime?

No, it is not an opposite but a different thing. There is a couple of Qt bugs closed as no-bugs: QTBUG-55449 and QTBUG-55510 that show the intent behind the feature. BTW, there is QTBUG-55510 a programmatic workaround for setting Qt DPI awareness without fixing qt.conf is provided (use at own discretion because it uses 'private' Qt implementation classes that change the interface without any notice with newer Qt version).

And you expressed the correct approach to do the scaling in per-monitor DPI aware mode. Unfortunately except that there is no much alternative at the time. There programmatic ways to assist the event handling for the window scaling when it is moved from one monitor to another, though. The method like resizeWidget (one, not many) at the head of this question should be called using something like (Windows):

// we assume MainWindow is the widget dragged from one monitor to another
bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
   MSG* pMsg = reinterpret_cast<MSG*>(message);

   switch (pMsg->message)
   {
      case WM_DPICHANGED:
         // parameters TBD but mind that 'last' DPI is in
         // LOWORD(pMsg->wParam) and you need to detect current
         resizeWidget(monitorRatio());
         break;

This is quite difficult and troublesome way to go and I resorted to enable the app to switch between System and Per Monitor DPI Aware modes by letting the user to choose the mode and restart the app process (either fixing qt.conf or doing workaround from QTBUG-55510 at the app start). Our hope is that Qt company realizes there is a need for per-monitor DPI aware mode with auto scaling for widgets. Why would we need it (?) is another question. In my case I have per-monitor rendering within own app widget canvas which supposed to be scaled.

At first, reading the comment to this question from @selbie I realized maybe there is a way to try to set QT_SCREEN_SCALE_FACTORS while the app starts:

QT_SCREEN_SCALE_FACTORS [list] specifies scale factors for each screen. This will not change the size of point sized fonts. This environment variable is mainly useful for debugging, or to work around monitors with wrong EDID information(Extended Display Identification Data).

I then read Qt blog on how to apply multiple screen factors and attempted to do the below for 4K and 1080p monitors where 4K is listed first (main).

qputenv("QT_SCREEN_SCALE_FACTORS", "2;1");

That does helps a bit: almost correct rendering but introduces defects with window size while moving the window from one monitor to another pretty much like QTBUG-55449 does. I guess I will go with WM_DPICHANGED + QT_SCREEN_SCALE_FACTORS approach if the customer considers current app behavior as a bug (we make the same base for all monitors DPI via System DPI Aware). Still there is no ready to use solution from Qt yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...