QML设计登陆界面

QML设计登陆界面


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


环境:

主机:WIN7

开发环境:Qt5.2


说明:

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


效果图:



源代码:

main.qml

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import QtQuick 2.0  
  2.   
  3. Rectangle  
  4. {  
  5.     id: login_gui  
  6.   
  7.     width: 320; height: 480  
  8.   
  9.     SystemPalette { id: activePalette }  
  10.   
  11.     //背景图片  
  12.     Image  
  13.     {  
  14.         id: background  
  15.         anchors { top: parent.top; bottom: parent.bottom }  
  16.         anchors.fill: parent  
  17.         source: "pics/pic1.png"  
  18.         fillMode: Image.PreserveAspectCrop  
  19.     }  
  20.   
  21.     //顶烂  
  22.     Item  
  23.     {  
  24.         id: top_bar  
  25.         width: login_gui.width; height: login_gui.height * 0.05  
  26.         anchors.top: login_gui.top  
  27.   
  28.         Text  
  29.         {  
  30.             id: title  
  31.             anchors { top: parent.top; horizontalCenter: parent.horizontalCenter }  
  32.             text: "登陆"  
  33.             font.bold: true  
  34.             font.pointSize: login_gui.height * 0.05 * 0.7  
  35.             color: "dark red"  
  36.         }  
  37.     }  
  38.   
  39.     //空白栏  
  40.     Item  
  41.     {  
  42.         id: space1  
  43.         width: login_gui.width; height: login_gui.height * 0.1  
  44.         anchors.top: top_bar.bottom  
  45.     }  
  46.   
  47.     //登陆框  
  48.     Rectangle  
  49.     {  
  50.         id: rect1  
  51.         width: login_gui.width * 0.8; height: login_gui.height * 0.3  
  52.         anchors { top: space1.bottom; horizontalCenter: parent.horizontalCenter }  
  53.         border.color: "#707070"  
  54.         color: "transparent"  
  55.   
  56.         LineInput  
  57.         {  
  58.             width: rect1.width * 0.8; height: rect1.height * 0.2  
  59.             font_size:height * 0.7  
  60.             anchors {horizontalCenter: rect1.horizontalCenter; top: rect1.top; topMargin: 8}  
  61.             hint: "请输入用户号"  
  62.         }  
  63.   
  64.         LineInput  
  65.         {  
  66.             width: rect1.width * 0.8; height: rect1.height * 0.2  
  67.             font_size:height * 0.7  
  68.             anchors {horizontalCenter: rect1.horizontalCenter; bottom: btn_login.top;  bottomMargin: rect1.height * 0.1}  
  69.             hint: "请输入密码"  
  70.         }  
  71.   
  72.         Button  
  73.         {  
  74.             id: btn_login  
  75.             width: rect1.width * 0.35; height: rect1.height * 0.2  
  76.             anchors { left: rect1.left; leftMargin: 28; bottom: rect1.bottom; bottomMargin: 8 }  
  77.             text: "登陆"  
  78.             //onClicked: SameGame.startNewGame()  
  79.         }  
  80.   
  81.         Button  
  82.         {  
  83.             id: btn_quit  
  84.             width: rect1.width * 0.35; height: rect1.height * 0.2  
  85.             anchors { right: rect1.right; rightMargin: 28; bottom: rect1.bottom; bottomMargin: 8 }  
  86.             text: "退出"  
  87.             //onClicked: SameGame.startNewGame()  
  88.         }  
  89.     }  
  90. }  

Button.qml

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import QtQuick 2.0  
  2.   
  3. Rectangle {  
  4.     id: container  
  5.   
  6.     property string text: "Button"  
  7.   
  8.     signal clicked  
  9.   
  10.     width: buttonLabel.width + 20; height: buttonLabel.height + 5  
  11.     border { width: 1; color: Qt.darker(activePalette.button) }  
  12.     antialiasing: true  
  13.     radius: 8  
  14.   
  15.     // color the button with a gradient  
  16.     gradient: Gradient {  
  17.         GradientStop {  
  18.             position: 0.0  
  19.             color: {  
  20.                 if (mouseArea.pressed)  
  21.                     return activePalette.dark  
  22.                 else  
  23.                     return activePalette.light  
  24.             }  
  25.         }  
  26.         GradientStop { position: 1.0; color: activePalette.button }  
  27.     }  
  28.   
  29.     MouseArea {  
  30.         id: mouseArea  
  31.         anchors.fill: parent  
  32.         onClicked: container.clicked();  
  33.     }  
  34.   
  35.     Text {  
  36.         id: buttonLabel  
  37.         anchors.centerIn: container  
  38.         color: activePalette.buttonText  
  39.         text: container.text  
  40.     }  
  41. }  

LineInput.qml

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import QtQuick 2.0  
  2.   
  3. FocusScope {  
  4.     id: wrapper  
  5.   
  6.     property alias text: input.text  
  7.     property alias hint: hint.text  
  8.     property alias prefix: prefix.text  
  9.     property int font_size: 18  
  10.   
  11.     signal accepted  
  12.   
  13.     Rectangle {  
  14.         anchors.fill: parent  
  15.         border.color: "#707070"  
  16.         color: "#c1c1c1"  
  17.         radius: 4  
  18.   
  19.         Text {  
  20.             id: hint  
  21.             anchors { fill: parent; leftMargin: 14 }  
  22.             verticalAlignment: Text.AlignVCenter  
  23.             text: "Enter word"  
  24.             font.pixelSize: font_size  
  25.             color: "#707070"  
  26.             opacity: input.length ? 0 : 1  
  27.         }  
  28.   
  29.         Text {  
  30.             id: prefix  
  31.             anchors { left: parent.left; leftMargin: 14; verticalCenter: parent.verticalCenter }  
  32.             verticalAlignment: Text.AlignVCenter  
  33.             font.pixelSize: font_size  
  34.             color: "#707070"  
  35.             opacity: !hint.opacity  
  36.         }  
  37.   
  38.         TextInput {  
  39.             id: input  
  40.             focus: true  
  41.             anchors { left: prefix.right; right: parent.right; top: parent.top; bottom: parent.bottom }  
  42.             verticalAlignment: Text.AlignVCenter  
  43.             font.pixelSize: font_size  
  44.             //color: "#707070"  
  45.             color: "black"  
  46.             onAccepted: wrapper.accepted()  
  47.         }  
  48.     }  
  49. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt QML是一种用于创建跨平台应用程序的框架,它结合了Qt的强大功能和QML的声明性语言。在Qt QML中,可以使用QML语言来设计用户界面,并通过JavaScript来处理逻辑。 要创建一个时间设置界面,可以按照以下步骤进行: 1. 创建一个QML文件,例如TimeSettings.qml。 2. 在QML文件中,使用Qt Quick Controls中的组件来构建界面。例如,可以使用TextField组件来输入时间,Button组件来保存设置等。 3. 使用Qt Quick Controls中的Dialog组件来创建一个对话框,用于显示时间设置界面。 4. 在QML文件中,使用JavaScript来处理逻辑。例如,可以使用信号和槽机制来响应按钮的点击事件,将输入的时间保存到变量中等。 5. 在主程序中,加载并显示TimeSettings.qml文件。 以下是一个简单的示例代码: ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 Dialog { id: timeSettingsDialog title: "Time Settings" standardButtons: Dialog.Ok | Dialog.Cancel Column { spacing: 10 TextField { id: timeInput placeholderText: "Enter time" } Button { text: "Save" onClicked: { // Save the time to a variable or perform other actions var time = timeInput.text; console.log("Time saved:", time); timeSettingsDialog.accept(); } } } } ``` 在主程序中,可以使用以下代码加载并显示时间设置界面: ```cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlComponent> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/TimeSettings.qml"))); if (component.status() == QQmlComponent::Error) { qWarning() << component.errorString(); return -1; } QObject *dialog = component.create(); if (dialog) { dialog->setProperty("visible", true); int result = app.exec(); delete dialog; return result; } return -1; } ``` 这样,当主程序运行时,会显示一个对话框,用户可以在其中输入时间并保存。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值