QtQuick 全局缩放和放大<DPI>

Qt提供了全局缩放,但是官方没有实例,不过折腾出来了.全局缩放和放大可以用在移动平台中。480dpi的设备放大3倍将变成160dpi。
关键点:
1.在QGuiApplication初始化之前调用缩放
2.禁用HilightDpi  QGuiApplication::setAttribute(Qt::AA_DisableHighDpiScaling);

3.安卓系统中获取缩放比例。(Qt可以获取缩放比例,但是必须在QGuiApplication初始化之后才可以,但是缩放必须在QGuiApplication初始化之前调用,因此矛盾,所以借助java)
缩放参数:1.0  1.5  2.0等等   分别代表 mdpi hdpi xhdpi等等...
坑:JAVA中获取全局缩放必须在Oncreate中进行,我在Qt5.7调用,发现Oncreate的时候main函数在另外一个线程中,因此,C++调用java应该如此:并等待OnCreate执行后才通知JAVA获取缩放比例。

#ifdef UseAndroid
int static Getdpi()
{
     __android_log_print(ANDROID_LOG_DEBUG,"hello","NDK:*********9999"); // LOG类型:debug


     qDebug()<<"C++获取DPI*****开始";


    QAndroidJniEnvironment   EV;
    jint   i =  QAndroidJniObject::callStaticMethod<jint>("org/qtproject/qt5/android/bindings/QtActivity","getdpi","()I");
    qDebug()<<"C++获取DPI=*****"<<(int)i;

    return  (int)i;
     return 0;
}


float static Getdensity()
{
     __android_log_print(ANDROID_LOG_DEBUG,"hello","NDK:*********Getdensity"); // LOG类型:debug


     qDebug()<<"C++获取DPI*****开始";


    QAndroidJniEnvironment   EV;
    jfloat   i =  QAndroidJniObject::callStaticMethod<jfloat>("org/qtproject/qt5/android/bindings/QtActivity","getdensity","()F");
    qDebug()<<"C++获取DPI=*****"<<(float)i;

    return  i;
}
#endif

void  RegFunc(QQmlApplicationEngine&  engine)
{
    #ifdef UseAndroid
    TQmlHelper*  tmp = new TQmlHelper( Getdpi(),Getdensity());
    #else
    TQmlHelper*  tmp = new TQmlHelper(0,0);
    #endif
        engine.rootContext()->setContextProperty("myapp", (QObject*)tmp);
}

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

      QGuiApplication::setApplicationName("Myapp");
    QGuiApplication::setOrganizationName("QtProject");
     QGuiApplication::setAttribute(Qt::AA_DisableHighDpiScaling);

     QString  s;
     float  f;
#ifdef UseAndroid
   float  di = 160.0;
//  必须使用float  否则会因语法规则无法获取小数点
    f  = Getdpi()/di;
#else
        //  如果是电脑 就当成1倍。。。。没办法 
        f  =  160/160;
#endif

     s.sprintf("%f",f);
     qDebug()<<"放大:"<<s;
     QByteArray  b = s.toStdString().c_str();
     qputenv("QT_SCALE_FACTOR", b); 
     QGuiApplication app(argc, argv);

     QQuickStyle::setStyle("Material");
   //



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

    return app.exec();
}

 #ifndef TQMLHELPER_H
#define TQMLHELPER_H

#include <QObject>
#include<QVariant>
class TQmlHelper : public QObject
{
    Q_OBJECT
public:
    explicit TQmlHelper(int dpi,float densioty,QObject *parent = 0);
    Q_INVOKABLE QVariant  getdpi();
    Q_INVOKABLE QVariant  getdensity();

signals:

public slots:

 private:
    int  dpi ;
    float density;
};

#endif // TQMLHELPER_H

#include "tqmlhelper.h"
***CPP:

TQmlHelper::TQmlHelper(int dpi,float densioty,QObject *parent) : QObject(parent)
{
        this->dpi = dpi;
        this->density = densioty;
}



QVariant  TQmlHelper::getdpi()
{
        return  this->dpi;
}

QVariant  TQmlHelper::getdensity()
{
        return  this->density;
}

QtActivity.java

  static int  mready = 0;//等待Oncreate执行
    static  int  mydpi  = 0;
    static  float  density  = 0;

    public QtActivity()
    {
        m_loader = new QtActivityLoader(this);
        if (Build.VERSION.SDK_INT >= 21) {
            QT_ANDROID_THEMES = new String[] {"Theme_Holo_Light"};
            QT_ANDROID_DEFAULT_THEME = "Theme_Holo_Light";
        } else {
            QT_ANDROID_THEMES = new String[] {"Theme_DeviceDefault_Light"};
            QT_ANDROID_DEFAULT_THEME = "Theme_DeviceDefault_Light";
        }

    }
    public  static  float  getdensity()
    {
        Log.i("hello", "jni*begin getdensity");
        while (mready==0) {
            try {
                Log.i("hello**", "jni*2****************sleep**********");
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        Log.i("hello", "jni*2***getdensity ok**********"+density);
        return density;
    }

    public  static  int  getdpi()
    {
        Log.i("hello**", "jni begin getdpi**************************");
        while (mready==0) {
            try {
                Log.i("hello**", "jni*****************sleep**********");
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        Log.i("hello**", "jni*2***************getdpi ok**********"+mydpi);
        return mydpi;
    }


    /////////////////////////// forward all notifications ////////////////////////////
    /////////////////////////// Super class calls ////////////////////////////////////
    /////////////// PLEASE DO NOT CHANGE THE FOLLOWING CODE //////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    //---------------------------------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
         displ = new  DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displ);
        Log.i("hello**", "jni*************Get**************"+displ.densityDpi);
        mydpi = displ.densityDpi;
        density = displ.density;
        m_loader.APPLICATION_PARAMETERS = APPLICATION_PARAMETERS;
        m_loader.ENVIRONMENT_VARIABLES = ENVIRONMENT_VARIABLES;
        m_loader.QT_ANDROID_THEMES = QT_ANDROID_THEMES;
        m_loader.QT_ANDROID_DEFAULT_THEME = QT_ANDROID_DEFAULT_THEME;
        mready = 1;
        m_loader.onCreate(savedInstanceState);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值