HMI-7-[高分屏支持]:Qt 适配高分屏

基于Qt的汽车仪表模拟

照例,还是先上效果图,宣传哈哈哈哈哈

今天学到一个新东西,就是Qt对高分屏的支持,即高DPI的支持,有个Qt写的软件在老板的SB2上运行出现了显示异常,因为老板的电脑分辨率是3000*2000 200的放大,导致软件不支持。所以在后面的软件中,需要对软件最高分屏支持,但是目前(5.10.0.版本)中,多高分屏的支持还是不是很完美呀。

这里需要注意,Qt是在5.6.0以后的版本才会支持高分屏的适配哈。

先说怎么搞

这个是没有加入高分屏适配的main.cpp文件

#include <QApplication>
#include <QDateTime>
#include "ControlPanel/controlpanel.h"
#include "Core/loader.h"
int main(int argc,char *argv[])
{
    QApplication a(argc,argv);
    Loader *loader = new Loader(":/Core/Resources/Core/LoaderImage.gif");
    loader->show();
    //装逼时刻放开


    QDateTime n=QDateTime::currentDateTime();
    QDateTime now;
    do
    {
        now=QDateTime::currentDateTime();
        a.processEvents();
    }while (n.secsTo(now)<=1);


    ControlPanel controlPanel;
    controlPanel.show();
    loader->finish(&controlPanel);
    delete loader;
    return a.exec();
}

这个是加入了高分屏适配的main.cpp文件

#include <QApplication>
#include <QDateTime>
#include "ControlPanel/controlpanel.h"
#include "Core/loader.h"
int main(int argc,char *argv[])
{

	#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
	#endif
    QApplication a(argc,argv);
    Loader *loader = new Loader(":/Core/Resources/Core/LoaderImage.gif");
    loader->show();
    //装逼时刻放开


    QDateTime n=QDateTime::currentDateTime();
    QDateTime now;
    do
    {
        now=QDateTime::currentDateTime();
        a.processEvents();
    }while (n.secsTo(now)<=1);


    ControlPanel controlPanel;
    controlPanel.show();
    loader->finish(&controlPanel);
    delete loader;
    return a.exec();
}

其实就是加入了一句话 ,有用的就是其中的一句,剩下的检查版本兼容的。

 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
	#endif

还有就是最中重要的!!!

一定要写在“QApplication a(argc,argv);”的前面

一定要写在“QApplication a(argc,argv);”的前面

一定要写在“QApplication a(argc,argv);”的前面

这里引用下灿哥哥博客中的内容:原文链接:https://blog.csdn.net/caoshangpa/article/details/60965690

但是Qt5.6.0对高分屏的支持存在bug,在Surface Pro 4上亲测:

1.在无边框程程序setWindowFlags(Qt::FramelessWindowHint);下调用this->showMaximized();并不能实现最大化。

2.在使用QWebEngine时,输入法候选框的位置会跑偏(不在输入框的下方)。

3.在使用QWebEngine时,当输入中途按退格键(BackSpace),第一个输入的字符不能被正常删除。

Qt5.7.1修复了这三个bug。

2019/08/21 00:21

发布了259 篇原创文章 · 获赞 535 · 访问量 55万+

猜你喜欢

转载自blog.csdn.net/z609932088/article/details/100047184