所有的热爱都要不遗余力,真正喜欢它便给它更高的优先级,和更多的时间吧!
QML其它文章请点击这里: QT QUICK QML 学习笔记
有时候打开软件时,是不是需要加载上一次关闭前的屏幕拉伸状态?是不是觉得用Sqlite又麻烦了点?别急QT Quick 提供了Setting,可以保存一些简单的信息,如用户名,密码,窗口位置,大小,还有控件的属性等,既简单又实用。我们先来看运行效果:
1. 运行效果
运行软件后,改变了三处状态:
- 改变了滑动的状态,从默认蓝色右侧移动到中间绿色
- 改变软件位置,中间——>左侧
- 改变软件大小,小——>大
重启软件,能恢复上一次关闭时的状态。
2. 代码
具体实例是在QT Quick QML 之 states和transitions介绍的基础上,新增状态保存QML文件。
RunStates.qml:
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow{
id: root;
visible: true
width: 300
height: 200
title: qsTr("状态保存");
///--新增代码1 :保存滑块的状态
property alias mainState: slider.state;
///--新增代码2: 调用WindowSaveState.qml
WindowSaveState {
window: root
_enabled: true
}
//整个横条
Rectangle {
id: rct
anchors.centerIn: parent
height: 30
width: 120
color: "gray"; //底层灰色
radius: height/2;
property var _strs: ["左边", "中间", "右边"];
//横条里的小滑块
Rectangle {
id: slider
width: parent.width * 0.33
radius: parent.radius
color: (slider.state == "") ? "red" : ((slider.state == "middle")? "green": "blue")
anchors {
top: parent.top
bottom: parent.bottom
}
state: "right" //初始状态在右边
states: [ //状态列表 有三种状态可选
//空字节,
//也可以只给一个名字如下:
//State { name: "left" },
State {
name: "middle"
PropertyChanges {
target: slider
x: slider.width//120-80*0.6=32;
}
},
State {
name: "right"
PropertyChanges {
target: slider
x: slider.width*2//80-80*0.6=32;
}
}
]
//transitions 转换
transitions: [
//""状态 ——> "middle"状态 200ms
Transition {
from: ""; to: "middle";
NumberAnimation{ properties: "x" ; duration:200 }
},
//"middle"状态 ——> "right"状态 500ms
Transition {
from: "middle";to: "right";
NumberAnimation{properties: "x"; duration:500}
},
//"其它"状态 ——> "其它" 状态 1000ms
Transition {
from: "*"; to: "*";
NumberAnimation{properties: "x"; duration:1000}
}
]
Text {
anchors.centerIn: parent
text: slider.state == "" ? rct._strs[0] : (slider.state == "middle"? rct._strs[1] : rct._strs[2]);
color: "white"
font.pointSize: 10
}
}
}
MouseArea {
anchors.fill: rct;
onPressed: {
if(slider.state == ""){
slider.state = "middle";
}
else if(slider.state == "middle"){
slider.state = "right";
}
else{
slider.state = "";
}
}
}
}
在之前的实例中新增两处代码,看过前面实例的只看新增两处代码就好,在新增代码1中 alias 属性也已经学过(QT Quick Qml 之property alias别名使用(Column和Repeater升级版本))。
● WindowSaveState.qml :
import QtQuick 2.7
import QtQuick.Controls 2.4
import QtQuick.Window 2.11
import Qt.labs.settings 1.0
Item {
property Window window
property bool _enabled
Settings {
id: setting
category: "MainWindowState"
property int x
property int y
property int width
property int height
property int visibility
property string state
}
Component.onCompleted: {
if (_enabled && setting.width && setting.height) {
window.x = setting.x;
window.y = setting.y;
window.width = setting.width;
window.height = setting.height;
window.visibility = setting.visibility;
window.mainState = setting.state;
}
}
Connections {
target: window
onXChanged: if(_enabled) saveSettingsTimer.restart()
onYChanged: if(_enabled) saveSettingsTimer.restart()
onWidthChanged: if(_enabled) saveSettingsTimer.restart()
onHeightChanged: if(_enabled) saveSettingsTimer.restart()
onVisibilityChanged: if(_enabled) saveSettingsTimer.restart()
onMainStateChanged: if(_enabled) saveSettingsTimer.restart()
}
Timer {
id: saveSettingsTimer
interval: 500
repeat: false
onTriggered: saveSettings()
}
function saveSettings() {
if(_enabled) {
switch(window.visibility) {
case ApplicationWindow.Windowed:
setting.x = window.x;
setting.y = window.y;
setting.width = window.width;
setting.height = window.height;
setting.visibility = window.visibility;
setting.state = window.mainState;
break;
case ApplicationWindow.FullScreen:
setting.visibility = window.visibility;
setting.state = window.mainState;
break;
case ApplicationWindow.Maximized:
setting.visibility = window.visibility;
setting.state = window.mainState;
break;
}
}
}
}
● main.cc 中新增三行:
app.setOrganizationName("Some Company");
app.setOrganizationDomain("somecompany.com");
app.setApplicationName("Amazing Application");
这是直接参考QT帮助手册的,具体公司什么的,可以按实际填写,我按照默认的也能保存。
参考1:QGroundControl地面站
参考2:QT Creator 帮助
QML其它文章请点击这里: QT QUICK QML 学习笔记