C++ QT qml 学习之 做个登录界面

最近在学习QT,也初探到qml 做ui 的灵活性与强大,于是手痒痒,做个demo 记录下学习成果

主要内容是如何自己编写一个按钮以及qml多窗口。

参考WX桌面版,做一个登录界面,这里面按钮是写的一个组合控件,有 按下,释放,以及正常 三种状态。

import QtQuick 2.0
import QtQuick.Controls 2.4



Rectangle
{
    id:root
    width:400
    height:50
    radius:6
    property alias text:rect_text.text
    property alias tip_text:btn_tip.text
    state: rect_mouse.pressed ? "pressed" : (rect_mouse.containsMouse ? "hovered" : "normal")
    //使用方可以处理这个信号来相应按钮点击
    signal btnclicked

    Text
    {
        id:rect_text
        font.pointSize: 16
        font.bold: true
        color:"#ffffff"
        anchors.horizontalCenter:parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        anchors.verticalCenterOffset: 0
    }

    ToolTip
    {
        id:btn_tip
        font.pointSize: 14
        delay: 500
        timeout: 2000
        x:parent.width/2
        y:parent.height
    }

    states: [
        State {
            name: "pressed"
            PropertyChanges { target: root; color: "#01968c" }
        },
        State {
            name: "normal"
            PropertyChanges { target: root; color: "#00beac" }
        },
        State {
            name: "hovered"
            PropertyChanges { target: root; color: "#20c9b3" }
        }
    ]

    MouseArea
    {
        id:rect_mouse
        anchors.fill: parent
        hoverEnabled: true //是否处理悬浮事件,默认false,只有按下鼠标键时才处理鼠标事件,为true时即使没有按下鼠标键也会作相应的处理
        preventStealing:true//默认为false,为true时可以防止当前鼠标事件被其它对象拦截。
        propagateComposedEvents:true//默认为 false,当设置为 true 时,就可以将事件传递给重叠的其他鼠标区域了
        enabled: true
        cursorShape: Qt.PointingHandCursor

        onPressed:
        {
            rect_text.anchors.verticalCenterOffset = 2
            state = "pressed"
        }

        onEntered:
        {
            if(btn_tip.text != "")
            {
                btn_tip.x = mouseX
                btn_tip.y = mouseY
                btn_tip.open()
                console.log(btn_tip.x,btn_tip.y)
            }
            state = "hovered"
        }

        onReleased:
        {
            if(rect_mouse.containsMouse)//鼠标在按钮范围才有效
                root.btnclicked()

            rect_text.anchors.verticalCenterOffset = 0
            state = "hovered"
        }

        onExited://鼠标离开时关闭
        {
            btn_tip.close()
            state = "normal"
        }
//        onClicked: {
//            //btnclicked()
//        }
    }
}

然后在新增一个ChatDlg.qml文件,里面写一个window 当作是登陆后的主界面

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.9


Window {
    id: chatWindow
    visible: true
    width: 800
    height: 600
    minimumWidth: 250
    minimumHeight: 150
    maximumWidth: 1000
    maximumHeight: 600
    color: "white"
    title: qsTr("聊天窗口")

    Row{
        id: layoutR
        anchors.centerIn: parent
        spacing: 5 //行或列的像素间隔

        Column {
            ListView {
                  id: chatGroup
                  width: 180
                  height: 500
                  //anchors.verticalCenter:   parent.verticalCenter
                  model: ["聊天1","聊天2","聊天3","聊天4","聊天5","聊天6","聊天7","聊天8","聊天9","聊天10","聊天11"]
                  spacing: 2
                  delegate: MyIconButton {
                      btn_txt: modelData
                  }
              }
        }

        Column {
            id: layoutC
            //anchors.centerIn: parent
            spacing: 5 //行或列的像素间隔
            Image {
                    id: m_Logo
                    width: 600
                    height: 250
                    anchors.left: chatGroup.right
                    fillMode: Image.PreserveAspectFit   //保持宽高比
                    source: "/new/img/048.jpg.emoji.jpg"
                    //anchors.verticalCenter:   parent.verticalCenter
                }
            Rectangle {
                        width: 600
                        height: 250
                        color: "lightgrey"
                        border.color: "grey"

                        TextEdit {
                            id: m_Input
                            width: 600
                            height: 250
                            //color: "#20c9b3"
                            //anchors.top: m_Logo.bottom
                            //anchors.horizontalCenter:   parent.horizontalCenter
                            //anchors.verticalCenter:   parent.verticalCenter
                            //anchors.left: chatGroup.right
                            //anchors.leftMargin: 5

                        }
                }

            //登录按钮
            MyButton {
                id: btnSend
                width: 168
                height: 36
                text: "发送"
                //tip_text: ""
                enabled: true
                onBtnclicked: {
                    //showAni.start()
                }
            }

        }



}


}

登录窗口设计如下:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4


Window {
    id: mainWind
    visible: true
    width: 300
    height: 500
    minimumWidth: 250
    minimumHeight: 150
    maximumWidth: 1000
    maximumHeight: 600
    color: "white"
    title: qsTr("WX")

    //头像
    Image {
            id: m_Logo
            sourceSize.height: 100
            sourceSize.width: 100
            fillMode: Image.PreserveAspectFit   //保持宽高比
            source: "/new/img/048.jpg.emoji.jpg"
            anchors.horizontalCenter: parent.horizontalCenter
            y: parent.height/6
        }

    Text {
        id: name
        text: qsTr("小熊猫")
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: m_Logo.bottom
        anchors.topMargin: 20
    }
    //登录后弹出聊天主界面
    ChatDlg {
        id: chatDialog
        visible: false

    }

    //登录按钮
    MyButton {
        id: btn1
        width: 168
        height: 36
        text: "登录"
        tip_text: "Login your account"
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: name.bottom
        anchors.topMargin: 50
        onBtnclicked: {
            chatDialog.visible = true
            mainWind.visible = false
            //showAni.start()
        }
    }
}

按下登录按钮后弹出主界面,隐藏登录界面

你学废了吗?

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用Qt框架开发应用时,我们可以使用C++QML进行数据的传递和界面的刷新。下面我将详细说明如何实现这个过程。 首先,在C++代码中,我们需要定义一个QObject派生的类,用于封装我们需要传递的数据。这个类需要使用Q_PROPERTY宏定义相关属性,并提供相应的读写方法。 接下来,在C++代码中,我们需要创建一个QQmlApplicationEngine对象,并加载我们的QML界面文件。然后,使用engine.rootObjects()函数获取根对象,可以将其设置为我们创建的QObject派生类的实例。 在QML文件中,我们可以使用绑定语法将C++中的属性绑定到QML界面上的相应组件。这样,当C++中的属性值发生变化时,QML界面上对应组件的显示也会更新。例如,可以使用bind的方式将C++中的一个整型属性绑定到QML界面上的Text组件的text属性上,即:Text { text: myObject.myProperty }。 当我们需要修改C++中的属性并刷新QML界面时,只需要直接通过对象的属性名称调用相应的写方法即可。例如,在C++代码中我们可以通过myObject.setMyProperty(newValue)的方式修改myProperty的值,然后QML界面上的Text组件就会显示该新值。 总结一下,传递数据并刷新QML界面的步骤为:定义QObject派生类用于封装数据,创建QQmlApplicationEngine并加载QML界面文件,将根对象设置为QObject派生类的实例,使用绑定语法将C++中的属性绑定到QML界面上的组件,通过调用C++对象的写方法修改属性值并实现界面刷新。这样,我们就可以实现C++QML之间的数据传递和界面刷新了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值