qt qml 高宽自动适配android设备

本篇主要是QML移植到android设备宽高的适配,其他设备没有尝试过。

字体和布局,网上其他的博客很清楚了

具体实现很简单,将在android设备上的尺寸分别除以在pc上设置的长宽得到长宽的比例,其他的就在长宽的比例中去一个中间值。

下面就用我最近写得模仿qq登录的界面中的运用

Window {
    id: main;
    color: "white";
    visible: true
    width: 360;//pc端的设置对android无影响
    height: 640;

<span style="white-space:pre">	</span>//除以得到系数
    property real multiplierH: main.height/640;
    property real multiplierW: main.width/360;
<span style="white-space:pre">	</span>//计算高的函数
    function dpH(numbers) {
           return numbers*multiplierH;
       }//计算宽的函数
   function dpW(numbers) {

       return numbers*multiplierW;
   }
<span style="white-space:pre">	</span>//平均值函数
   function dpX(numbers){
       return (dpW(numbers)+dpH(numbers))/2;
   }
    
    Rectangle{
        id: bg_rect;
        anchors.fill: parent;
        color: "#ecedf1";
        z: 0;
    }

    Column{

        width: parent.width;
        height: dpH(150);//运用
        x: 0;
        y: dpH(100);
        TextField{

            id :username_TF;

            x: 0;
            y: 0;
            z: 1;
            width: dpW(parent.width);
            height: dpH(30);

            placeholderText: "QQ号/手机号/邮箱";


            style:TextFieldStyle{

                background: Rectangle {
                            color: "white";
                        }
                }

        }



        TextField{

            id :password_TF;

            x: 0;
            y: dpH(30);
            z: 1;
            width: dpW(parent.width);
            height: dpH(30);
            echoMode: 2;
            placeholderText: "密码";

            style:TextFieldStyle{
                background: Rectangle {
                            color: "white";
                        }
                }

        }

        Rectangle{
            id: line_rect;
            color: "#eaecee";
            height: dpH(1);
            width: parent.width;
            x: 0;
            y: dpH(30);
            z: 1;
        }

        Rectangle {
            id: login_button;
            anchors.left: parent.left;
            anchors.leftMargin: dpW(10);
            anchors.bottom: parent.bottom;
            anchors.bottomMargin: dpH(50);


            color: "#1eb9f2";
            width: dpW(340);
            height: dpH(30);
            radius: dpX(4);
            border.color: "#1eb9f2";
            border.width: dpX(1);

            Text{
                id: login_button_text;
                anchors.centerIn: parent;

                color: "white";
                font.family: "微软雅黑";
                text: "登 录";
            }

           
            }
        }
    

}


效果如下

android端的


pc端效果


可能还有更简单的方法 希望对你有帮助

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值