QML與C++互動 登陸介面設計
阿新 • • 發佈:2018-11-15
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
QML與C++互動:登陸介面設計
本文部落格連結:http://blog.csdn.net/jdh99,作者:jdh,轉載請註明.
環境:
主機:WIN7
開發環境:Qt5.2.1
說明:
QML設計前臺介面,C++後臺負責邏輯
效果圖:
原始碼:
前臺qml檔案
login.qml
/********************************************************************** 登陸介面qml檔案* (c)copyright 2014,jdh* All Right Reserved*新建日期:2014/4/29 by jdh*修改日期:2014/4/30 by jdh*修改日期:2014/5/4 by jdh*修改日期:2014/5/5 by jdh**********************************************************************/ import QtQuick 2.0import "content"import Login_Gui 1.0Rectangle{ id: login width: 320; height: 512 SystemPalette { id: activePalette } //C++元件:使用者介面 Login_Gui { id :login_gui onSig_login_result: { //關閉登陸動畫 load_gif.opacity = 0 //根據登陸結果處理 switch (result) { //登陸成功 case 0: message.text = "登陸成功" message.opacity = 1 break; //無此使用者名稱 case 1: message.text = "登陸失敗:無此使用者名稱" message.opacity = 1 break; //密碼錯誤 case 2: message.text = "登陸失敗:密碼錯誤" message.opacity = 1 break; //達到最大登陸次數 case 3: message.text = "登陸失敗:達到最大登陸次數" message.opacity = 1 break; } } } //背景圖片 Image { id: background anchors { top: parent.top; bottom: parent.bottom } anchors.fill: parent source: "pics/pic1.png" fillMode: Image.PreserveAspectCrop } //訊息框 Message { id: message font_size: login.height * 0.03 anchors {centerIn: parent} opacity: 0 } //登陸動畫 AnimatedImage { id: load_gif; source: "pics/load.gif" anchors {horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter} z: 100 opacity: 0 } //頂欄 Item { id: top_bar width: login.width; height: login.height * 0.06 anchors.top: parent.top Text { id: title anchors { top: parent.top; horizontalCenter: parent.horizontalCenter } //text: "登陸" text: "登陸" font.bold: true font.pointSize: login.height * 0.06 * 0.4 color: "dark red" } } //空白欄 Item { id: space1 width: login.width; height: login.height * 0.1 anchors.top: top_bar.bottom } //登陸框 Rectangle { id: rect1 width: login.width * 0.8; height: login.height * 0.3 anchors { top: space1.bottom; horizontalCenter: parent.horizontalCenter } border.color: "#707070" color: "transparent" radius: 8 Row { spacing: rect1.width * 0.05 Item { width: rect1.width * 0.05; height: rect1.height } Column { spacing: rect1.height * 0.025 Item { width: rect1.width * 0.8; height: rect1.height * 0.05 } LineInput { id: txt_user_id 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: "請輸入使用者號" text:login_gui.user_id } LineInput { id: txt_password 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: "請輸入密碼" text:login_gui.password } Row { spacing: rect1.width * 0.1 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: login_req() } 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: { Qt.quit(); } } } Row { spacing: rect1.width * 0.1 CheckBox { id: check1 width: rect1.width * 0.35; height: rect1.height * 0.2 //anchors { left: rect1.left; top: rect1.bottom } caption: "記住密碼" selected: login_gui.flag_remember } CheckBox { id: check2 width: rect1.width * 0.35; height: rect1.height * 0.2 //anchors { right: rect1.right; top: rect1.bottom } caption: "自動登陸" selected: login_gui.flag_auto } } } } } //android自帶鍵處理 FocusScope { focus: true Keys.onReleased: { if (event.key == Qt.Key_Back) { console.log("qml login quit") login.sig_btn_quit() } } } //登陸請求函式 function login_req() { //判斷使用者名稱是否有效 if (txt_user_id.text == "") { message.text = "請輸入使用者名稱" message.opacity = 1 return } //判斷密碼是否有效 if (txt_password.text == "") { message.text = "請輸入密碼" message.opacity = 1 return } //顯示登陸動畫 load_gif.opacity = 1 //登陸請求 login_gui.user_id = txt_user_id.text login_gui.password = txt_password.text login_gui.flag_remember = check1.selected login_gui.flag_auto = check2.selected login_gui.slot_login_req() }// //訊號槽繫結// Component.onCompleted:// {// login_gui.sig_user_id_changed.connect(login_gui.slot_btn_login)// }}
後臺C++程式碼
main.c
/********************************************************************** 主檔案* (c)copyright 2014,jdh* All Right Reserved*新建日期:2014/1/27 by jdh*修改日期:2014/1/28 by jdh*修改日期:2014/2/4 by jdh*修改日期:2014/2/18 by jdh*修改日期:2014/2/27 by jdh*修改日期:2014/2/28 by jdh*修改日期:2014/3/1 by jdh*修改日期:2014/4/10 by jdh*修改日期:2014/5/4 by jdh**********************************************************************/#include "world.h"#include "main_gui.h"#include "login_gui.h"#include "light_gui.h"#include "heart_beat.h"#include "net.h"#include "data_sync_center.h"#include "set_ctrl_state.h"int main(int argc, char *argv[]){ QGuiApplication app(argc, argv); //註冊元件到QML qmlRegisterType<Login_Gui>("Login_Gui", 1, 0, "Login_Gui"); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/SH_User/login.qml")); viewer.showExpanded(); return app.exec();}
login_gui.h
/********************************************************************** 登陸介面模組標頭檔案* (c)copyright 2014,jdh* All Right Reserved*新建日期:2014/1/29 by jdh*修改日期:2014/2/1 by jdh*修改日期:2014/2/18 by jdh*修改日期:2014/3/18 by jdh*修改日期:2014/5/4 by jdh*修改日期:2014/5/5 by jdh*修改日期:2014/5/13 by jdh**********************************************************************/#ifndef LOGIN_GUI_H#define LOGIN_GUI_H/********************************************************************** 標頭檔案**********************************************************************/#include "world.h"/********************************************************************** 巨集定義**********************************************************************//********************************************************************** 登入間隔*單位:ms**********************************************************************/#define INTERVAL_LOGIN 500/********************************************************************** 最大登入次數**********************************************************************/#define NUM_LOGIN 5/********************************************************************** 資料結構**********************************************************************//********************************************************************** 登入介面類**********************************************************************/class Login_Gui : public QObject{ Q_OBJECT //屬性:使用者名稱 Q_PROPERTY(QString user_id READ user_id WRITE set_user_id NOTIFY sig_user_id_changed) //屬性:密碼 Q_PROPERTY(QString password READ password WRITE set_password NOTIFY sig_password_changed) //屬性:記住密碼標誌 Q_PROPERTY(bool flag_remember READ flag_remember \ WRITE set_flag_remember NOTIFY sig_flag_remember_changed) //屬性:自動登入標誌 Q_PROPERTY(bool flag_auto READ flag_auto \ WRITE set_flag_auto NOTIFY sig_flag_auto_changed)public: /********************************************************************* * 函式 **********************************************************************/ /********************************************************************* * 初始化函式 **********************************************************************/ Login_Gui(); /********************************************************************* * 解構函式 **********************************************************************/ ~Login_Gui(); /********************************************************************* * 屬性讀取:使用者號 **********************************************************************/ QString user_id(); /********************************************************************* * 屬性寫入:使用者號 **********************************************************************/ void set_user_id(QString str); /********************************************************************* * 屬性讀取:密碼 **********************************************************************/ QString password(); /********************************************************************* * 屬性寫入:密碼 **********************************************************************/ void set_password(QString str); /********************************************************************* * 屬性讀取:記住密碼標誌 **********************************************************************/ bool flag_remember(); /********************************************************************* * 屬性寫入:記住密碼標誌 **********************************************************************/ void set_flag_remember(bool flag); /********************************************************************* * 屬性讀取:自動登陸標誌 **********************************************************************/ bool flag_auto(); /********************************************************************* * 屬性寫入:自動登陸標誌 **********************************************************************/ void set_flag_auto(bool flag);signals: /********************************************************************* * 屬性改變訊號:使用者號 **********************************************************************/ void sig_user_id_changed(); /********************************************************************* * 屬性改變訊號:密碼 **********************************************************************/ void sig_password_changed(); /********************************************************************* * 屬性改變訊號:記住密碼標誌 **********************************************************************/ void sig_flag_remember_changed(); /********************************************************************* * 屬性改變訊號:自動登陸標誌 **********************************************************************/ void sig_flag_auto_changed(); /********************************************************************* * 訊號:登陸結果 *引數:result:0:成功 * 1:無此使用者名稱 * 2:密碼錯誤 * 3:達到登陸的最大次數 **********************************************************************/ void sig_login_result(int result); /********************************************************************* * 傳送網路幀 *引數:id:使用者名稱 * password:密碼 * cmd:幀命令 * index:傳送序列號 * frame:傳送的報文 **********************************************************************/ void sig_net_tx_frame_with_id(uint32_t id,uint32_t password,int cmd,uint16_t index,QByteArray frame);public slots: /********************************************************************* * 槽函式:登陸請求 **********************************************************************/ void slot_login_req(); /********************************************************************* * 槽函式:登陸響應 *引數:data:接收的資料 **********************************************************************/ void slot_login_ack(QByteArray data);private slots: /********************************************************************* * 槽函式:心跳滴答函式 *說明:1滴答觸發1次 **********************************************************************/ void slot_tick();private: /********************************************************************* * 變數 **********************************************************************/ /********************************************************************* * 屬性:使用者號 **********************************************************************/ QString _user_id; /********************************************************************* * 屬性:密碼 **********************************************************************/ QString _password; /********************************************************************* * 屬性:記住密碼標誌 **********************************************************************/ bool _flag_remember; /********************************************************************* * 屬性:自動登入標誌 **********************************************************************/ bool _flag_auto; /********************************************************************* * 滴答定時器 **********************************************************************/ QTimer *timer; /********************************************************************* * 登入計數器 **********************************************************************/ int Login_Counter;};#endif // LOGIN_GUI_H
login_gui.c
/********************************************************************** 登陸介面模組主檔案* (c)copyright 2014,jdh* All Right Reserved*新建日期:2014/1/29 by jdh*修改日期:2014/2/1 by jdh*修改日期:2014/2/17 by jdh*修改日期:2014/2/18 by jdh*修改日期:2014/2/16 by jdh*修改日期:2014/5/4 by jdh*修改日期:2014/5/5 by jdh*修改日期:2014/5/13 by jdh**********************************************************************//********************************************************************** 標頭檔案**********************************************************************/#include "login_gui.h"/********************************************************************** 函式**********************************************************************//********************************************************************** 初始化函式**********************************************************************/Login_Gui::Login_Gui(){ //初始化變數 Login_Counter = 0; //滴答初始化 timer = new QTimer(this); //繫結訊號槽 connect(timer, SIGNAL (timeout()), this , SLOT(slot_tick())); QFile file_cfg("cfg.txt"); QByteArray arr; bool ok; int flag_remember = 0; int flag_auto_login = 0; int id = 0; int password = 0; QString str; int i = 0; int j = 0; //屬性初始化 _user_id = ""; _password = ""; _flag_remember = false; _flag_auto = false; //判斷檔案是否存在 if (!file_cfg.exists()) { file_cfg.close(); } else { //檔案存在 file_cfg.open(QIODevice::ReadOnly); //讀取檔案 do { str.clear(); arr = file_cfg.readLine(); for (i = 0;i < arr.count();i++) { if ((arr.at(i) >= '0' && arr.at(i) <= '9') || \ (arr.at(i) >= 'a' && arr.at(i) <= 'f') || \ arr.at(i) == 'x') { str[j++] = arr.at(i); } } flag_remember = str.toInt(&ok,16); if (!ok) { break; } str.clear(); arr = file_cfg.readLine(); for (i = 0;i < arr.count();i++) { if ((arr.at(i) >= '0' && arr.at(i) <= '9') || \ (arr.at(i) >= 'a' && arr.at(i) <= 'f') || \ arr.at(i) == 'x') { str[j++] = arr.at(i); } } flag_auto_login = str.toInt(&ok,16); if (!ok) { break; } str.clear(); arr = file_cfg.readLine(); for (i = 0;i < arr.count();i++) { if ((arr.at(i) >= '0' && arr.at(i) <= '9') || \ (arr.at(i) >= 'a' && arr.at(i) <= 'f') || \ arr.at(i) == 'x') { str[j++] = arr.at(i); } } id = str.toInt(&ok,16); if (!ok) { break; } str.clear(); arr = file_cfg.readLine(); for (i = 0;i < arr.count();i++) { if ((arr.at(i) >= '0' && arr.at(i) <= '9') || \ (arr.at(i) >= 'a' && arr.at(i) <= 'f') || \ arr.at(i) == 'x') { str[j++] = arr.at(i); } } password = str.toInt(&ok,16); if (!ok) { break; } //判斷是否記住密碼 if (flag_remember == VALID_FLAG) { _user_id = QString::number(id,10); _password = QString::number(password,10); _flag_remember = true; //判斷是否自動登入 if (flag_auto_login == VALID_FLAG) { _flag_auto = true; &nb