QML设计登陆界面

QML设计登陆界面


本文博客链接:http://blog.youkuaiyun.com/jdh99,作者:jdh,转载请注明.


环境:

主机:WIN7

开发环境:Qt5.2


说明:

用QML设计一个应用的登陆界面


效果图:



源代码:

main.qml

import QtQuick 2.0

Rectangle
{
    id: login_gui

    width: 320; height: 480

    SystemPalette { id: activePalette }

    //背景图片
    Image
    {
        id: background
        anchors { top: parent.top; bottom: parent.bottom }
        anchors.fill: parent
        source: "pics/pic1.png"
        fillMode: Image.PreserveAspectCrop
    }

    //顶烂
    Item
    {
        id: top_bar
        width: login_gui.width; height: login_gui.height * 0.05
        anchors.top: login_gui.top

        Text
        {
            id: title
            anchors { top: parent.top; horizontalCenter: parent.horizontalCenter }
            text: "登陆"
            font.bold: true
            font.pointSize: login_gui.height * 0.05 * 0.7
            color: "dark red"
        }
    }

    //空白栏
    Item
    {
        id: space1
        width: login_gui.width; height: login_gui.height * 0.1
        anchors.top: top_bar.bottom
    }

    //登陆框
    Rectangle
    {
        id: rect1
        width: login_gui.width * 0.8; height: login_gui.height * 0.3
        anchors { top: space1.bottom; horizontalCenter: parent.horizontalCenter }
        border.color: "#707070"
        color: "transparent"

        LineInput
        {
            width: rect1.width * 0.8; height: rect1.height * 0.2
            font_size:height * 0.7
            anchors {horizontalCenter: rect1.horizontalCenter; top: rect1.top; topMargin: 8}
            hint: "请输入用户号"
        }

        LineInput
        {
            width: rect1.width * 0.8; height: rect1.height * 0.2
            font_size:height * 0.7
            anchors {horizontalCenter: rect1.horizontalCenter; bottom: btn_login.top;  bottomMargin: rect1.height * 0.1}
            hint: "请输入密码"
        }

        Button
        {
            id: btn_login
            width: rect1.width * 0.35; height: rect1.height * 0.2
            anchors { left: rect1.left; leftMargin: 28; bottom: rect1.bottom; bottomMargin: 8 }
            text: "登陆"
            //onClicked: SameGame.startNewGame()
        }

        Button
        {
            id: btn_quit
            width: rect1.width * 0.35; height: rect1.height * 0.2
            anchors { right: rect1.right; rightMargin: 28; bottom: rect1.bottom; bottomMargin: 8 }
            text: "退出"
            //onClicked: SameGame.startNewGame()
        }
    }
}

Button.qml

import QtQuick 2.0

Rectangle {
    id: container

    property string text: "Button"

    signal clicked

    width: buttonLabel.width + 20; height: buttonLabel.height + 5
    border { width: 1; color: Qt.darker(activePalette.button) }
    antialiasing: true
    radius: 8

    // color the button with a gradient
    gradient: Gradient {
        GradientStop {
            position: 0.0
            color: {
                if (mouseArea.pressed)
                    return activePalette.dark
                else
                    return activePalette.light
            }
        }
        GradientStop { position: 1.0; color: activePalette.button }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: container.clicked();
    }

    Text {
        id: buttonLabel
        anchors.centerIn: container
        color: activePalette.buttonText
        text: container.text
    }
}

LineInput.qml

import QtQuick 2.0

FocusScope {
    id: wrapper

    property alias text: input.text
    property alias hint: hint.text
    property alias prefix: prefix.text
    property int font_size: 18

    signal accepted

    Rectangle {
        anchors.fill: parent
        border.color: "#707070"
        color: "#c1c1c1"
        radius: 4

        Text {
            id: hint
            anchors { fill: parent; leftMargin: 14 }
            verticalAlignment: Text.AlignVCenter
            text: "Enter word"
            font.pixelSize: font_size
            color: "#707070"
            opacity: input.length ? 0 : 1
        }

        Text {
            id: prefix
            anchors { left: parent.left; leftMargin: 14; verticalCenter: parent.verticalCenter }
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: font_size
            color: "#707070"
            opacity: !hint.opacity
        }

        TextInput {
            id: input
            focus: true
            anchors { left: prefix.right; right: parent.right; top: parent.top; bottom: parent.bottom }
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: font_size
            //color: "#707070"
            color: "black"
            onAccepted: wrapper.accepted()
        }
    }
}



### 使用 QML 创建界面的工具与方法 #### 1. Qt Creator 集成开发环境 Qt Creator 是官方提供的集成开发环境(IDE),它为 QML 提供了强大的支持,包括代码编辑、实时预览和调试功能。开发者可以通过 Qt Creator 的设计视图直接拖放组件来创建用户界面[^2]。 #### 2. QML 文件的基本结构 QML 文件通常由导入语句、根对象和子对象组成。以下是一个简单的 QML 文件示例,展示了如何定义一个包含文本的矩形界面[^3]: ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 640 height: 480 title: "QML 示例" Rectangle { id: root color: "lightblue" anchors.fill: parent Text { text: "欢迎使用 QML!" anchors.centerIn: parent font.pixelSize: 24 } } } ``` #### 3. 动态创建界面元素 除了通过 QML 文件定义界面外,还可以在运行时动态创建界面元素。例如,可以使用 `Loader` 组件加载其他 QML 文件,或者通过 C++ 代码动态生成 QML 对象[^1]。 以下是通过 C++ 动态创建 QML 对象的示例: ```cpp QString qmlCode = "Rectangle { width: 50; height: 50; color: 'blue' }"; QQmlComponent component(&engine); component.setData(qmlCode.toUtf8(), QUrl()); QObject *object = component.create(); if (object) { QQmlContext *context = engine.rootContext(); context->setContextProperty("myRect", object); QQuickItem *item = qobject_cast<QQuickItem*>(object); item->setParentItem(engine.rootObjects().at(0)); } ``` #### 4. 使用 JavaScript 实现交互逻辑 QML 内置对 JavaScript 的支持,允许开发者在 QML 文件中直接编写交互逻辑。例如,以下代码展示了一个按钮点击事件的处理方式[^2]: ```qml Button { text: "点击我" onClicked: { console.log("按钮被点击了!") } } ``` #### 5. QML 设计工具 除了手动编写 QML 代码外,还有一些专门的工具可以帮助设计 QML 界面: - **Qt Design Studio**:这是一个专门用于设计 QML 用户界面的工具,支持拖放式界面设计,并能够与 Qt Creator 集成。 - **Qt Quick Designer**:这是 Qt Creator 中内置的一个模块,允许开发者通过图形化界面设计 QML 界面。 #### 6. QML 模块与组件库 为了加速开发,可以使用 Qt 提供的标准组件库,例如 `QtQuick.Controls` 和 `QtGraphicalEffects`。这些模块提供了丰富的控件和视觉效果,可以直接在 QML 文件中导入并使用[^3]。 ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 640 height: 480 title: "QML 控件示例" Column { anchors.centerIn: parent spacing: 10 Button { text: "按钮" } CheckBox { text: "复选框" } Slider { from: 0 to: 100 value: 50 } } } ``` ### 注意事项 - 在设计复杂界面时,建议将界面拆分为多个 QML 文件,以便于维护和重用。 - 使用 `Loader` 组件可以实现动态加载 QML 文件,从而减少初始加载时间[^2]。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值