Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063009
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类: C/C++

2011-02-27 22:21:28

我们有时候需要在QML 的程序中当 初始化一些 变量, 例如,我的QML GUI 中有个输入框,当上次输入后,程序退出时,我们的程序要保存这些输入。当下次重新启动时,我们希望再次加载上次的输入。

为了达到加载输入的我们希望QML能够提供一个 初始化的函数, 就像C++中的 构造函数。
上星期,我看同事在这问题上是编写了一个一个类A, 然后把这个类A导入到qml中,在qml中创建一个对象a,然后再在main函数中调用这个找到A类的对象a,然后调用a的函数来完成初始化。

今天我google下,发现通过 Component.onCompleted: {} 可以实现 上面所说的功能。 当这个文件 onCompleted 时会我们就可以触发一些动作(我们就可以做些初始化的工作)。
下面是我今天看到的代码。 帖上来供参考
==========================================================
%28QML%29

How-to create a persistent settings database in Qt Quick (QML)

As applications are opened and closed, it is often necessary to keep a few settings or data persistent, for example usernames and passwords, or simply configuration values. Qt Quick (QML) does not (yet?) have a way to access local files (this is doable through a plugin for example), but offers the Offline Storage API. This storage uses Sqlite databases to store data to disk.

Example Code

Here is an example on how to use this Offline Storage API (see ) to create a simple settings database. Each setting is composed of a key (represented as a string) and a value (also a string).

Let's create a small javascript file containing the functions necessary to store and retrieve our settings. This file can then be imported in our QML documents to be used. We will have 3 main functions: initialize() to set up the necessary tables, setSetting(setting,value) to record a setting in the database and getSetting(setting) to retrieve a setting's value.

//storage.js
// First, let's create a short helper function to get the database connection
function getDatabase() {
return openDatabaseSync("MyAppName", "1.0", "StorageDatabase", 100000);
}
 
// At the start of the application, we can initialize the tables we need if they haven't been created yet
function initialize() {
var db = getDatabase();
db.transaction(
function(tx) {
// Create the settings table if it doesn't already exist
// If the table exists, this is skipped
tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
});
}
 
// This function is used to write a setting into the database
function setSetting(setting, value) {
// setting: string representing the setting name (eg: “username”)
// value: string representing the value of the setting (eg: “myUsername”)
var db = getDatabase();
var res = "";
db.transaction(function(tx) {
var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
//console.log(rs.rowsAffected)
if (rs.rowsAffected > 0) {
res = "OK";
} else {
res = "Error";
}
}
);
// The function returns “OK” if it was successful, or “Error” if it wasn't
return res;
}
// This function is used to retrieve a setting from the database
function getSetting(setting) {
var db = getDatabase();
var res="";
db.transaction(function(tx) {
var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
if (rs.rows.length > 0) {
res = rs.rows.item(0).value;
} else {
res = "Unknown";
}
})
// The function returns “Unknown” if the setting was not found in the database
// For more advanced projects, this should probably be handled through error codes
return res
}

Now, we can use this short library in our QML files. Here is a short example:

import Qt 4.7
import "storage.js" as Storage
Rectangle {
width: 360
height: 360
id: screen
Text {
id: textDisplay
anchors.centerIn: parent
}
Component.onCompleted: {
// Initialize the database
Storage.initialize();
// Sets a value in the database
Storage.setSetting("mySetting","myValue");
// Sets the textDisplay element's text to the value we just set
textDisplay.text = "The value of mySetting is:\n" + Storage.getSetting("mySetting");
}
}
阅读(6307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~