Q: Loading colors from QML offline database ?
-
Hello there,
I have this little problem which I got working earlier - but something went terribly wrong and I lost the example file.
So, the actual problem is that I was testing this offline database system by a very simple way. When user selects a new color for the header, it changes -- no problem with that. But the trick is that when the user loads the application it does not load even the default value for the color.
Here are the functions to load data from the offlien database:
@// "Database.js"function _Load_ColorTheme ()
{
openDatabaseSync("ColorThemeDb", "1.0", "Color Theme", 1000000).transaction(function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS Themes (ColorString TEXT)') var _Theme = tx.executeSql('SELECT * FROM Themes') var _ColorTheme = "" var _Color = "" for (var i = 0; i < _Theme.rows.length; i++) { _ColorTheme = _Theme.rows.item(i).ColorString } if (!_ColorTheme) _Color = "#0099CC" else _Color = _ColorTheme return _Color } )
}@
If I use 'console.log(_Color)' in '_Load_ColorTheme()' function, I get the proper value (which is '#0099CC').
When I try to use it in a label's color (shown below) it does not load any color and it gives me the following error: "Unable to assign [undefined] to QColor color". Here's the examples how I've tried to use it:
@// MainPage.qml
import "Database.js" as Database
Rectangle
{
anchors.centerIn: parent
width: 250; height 250
color: Database._Load_ColorTheme()
}@I've also tried this - but it ain't working neither.
import "Database.js" as Database
Label
{
id: _ColorLabel
text: Database._Load_ColorTheme()
visible: false
}Rectangle
{
anchors.centerIn: parent
width: 250; height 250
color: _ColorLabel.text
}@Any solutions, advices...? Much appreciated!
Greetings,
Heikki Kullas
Oh, and just for you to know I do have else in the project. I have imported 'com.nokia.meego' and all the other stuffs. So that will not be a problem. :)
And I've tried to call a new property as a string and as well as a color with the returning value of the '_Load_ColorTheme()' value.
@// MainPage.qml
import "Database.js" as Database
property string _ColorString: Database._Load_ColorTheme()
// property color _ColorString: Database._Load_ColorTheme()
Rectangle
{
anchors.centerIn: parent
width: 250; height 250
color: _ColorString
}@ -
Hmm, I think you're right. I never thought it wouldn't work because I've been able to return the value on web projects for example.
I tried this on my MainPage.qml:
@Component.onCompleted: console.log(Database._Load_ColorTheme())@And guess what... It gives me 'undefined'!
So how I'm supposed to do this then? I got an idea, that I could use 'Label' or 'Text' components, and load the new color from a 'text' variable.
But after all, this should work like a Javascript in any other situation, right .. ? So what's going wrong here?