Qt Windows DPI adaptation

First of all, you have to make sure whether you are using Qt5 or Qt6. As far as I know, Qt6 can adapt the dpi by itself, but some displays will have bugs, such as the input box will suddenly become transparent. But Qt6 can lock the dpi by itself, and it will not change with the zooming in and out of the system. I don't have this code yet. If you need it, you can find it online.

PS: The default dpi of Qt5 is locked, and it will not affect the interface changes as the system zooms in/out, so we need to set the dpi adaptation. The development environment of this article is Qt5.15.2 + msvc2019 32bit version.

First go to the link, this article uses method four:

        Interface exception handling caused by QT windows dpi changes - Programmer Sought

After adopting method 4, the program can already adapt to the dpi, and the font will not be blurred, but if you do not prepare a high-resolution picture, the picture will also be blurred. Even if you prepare a high-definition picture, take pushbutton as an example, if you setIcon directly, the picture is still blurry (this should be a bug in Qt5, Qt6 will not be like this), you need to set the picture through the style sheet, so that you can set it for the pushbutton Set HD image:

poPb->setStyleSheet(QString("QPushButton{border-image: url(%1)}").arg(oPath));

If QLabel sets a high-definition image, you need to set the image size to the QLabel size:

        QPixmap p(filePath);
        p.scaled(poLb->size(), Qt::KeepAspectRatio);
        poLb->setScaledContents(true);
        poLb->setPixmap(p);

Je suppose que tu aimes

Origine blog.csdn.net/qq_25704799/article/details/124603528
conseillé
Classement