How to Read Textline from File to Global Variable/Text-String?

How to Read Textline from File to Global Variable/Text-String?

This is my first Project with Qt and Sailfish SDK and some things are seemed very confusing and not to be straight forward like i know that from LUA-Coding.

I found out read a textline can be reached with this code:

#include QFile
#include QString
#include QTextStream
QFile file("/home/defaultuser/title.txt");
QTextStream in(&file);
QString title = in.readLine();

But where to place it? Outside or Inside that main-function in project.cpp?:

int main(int argc, char *argv[])
{…}

And will “title” be a global Variable/Text-String that i can use in CoverPage.qml too?:

Label {
x: Theme.horizontalPageMargin
width: parent.width - 2*x
text: title
color: Theme.highlightColor
font.bold: true
wrapMode: Text.WordWrap
font.pixelSize: Theme.fontSizeExtraSmall
}

There’s a pretty good overview here:

You don’t have to use C++, you can also use python for many things:

If you come for lua, you might find doing it with python faster for your purposes. It’s also possible to develope (edit python, qml) directly on the device if you’re so inclined.

Thanks for the links, i try the python-way, it looks less complicated/horrible and more like the easy LUA-Workflow for quick prototyping.

One neat trick with qsettings is this:

#include <QtCore/QSettings>
QString osVersion  = QSettings("/etc/os-release", QSettings::IniFormat).value("VERSION_ID").toString();

which, despite the IniFormat specifyer works on simple key=value files (such as shell-compatible ones like /etc/os-release above), and avoids dealing with files and iostreams and such.

2 Likes

Consider passing native string to QML as a context property, e.g.

    QQuickView* view = SailfishApp::createView();
    view->rootContext()->setContextProperty("Title", title);
2 Likes

QQuickView* view = SailfishApp::createView();
view->rootContext()->setContextProperty(“Title”, title);

no matching member function for call to ‘setContextProperty’

#include <QtCore/QSettings>
QString osVersion = QSettings("/etc/os-release", QSettings::IniFormat).value(“VERSION_ID”).toString();

unused QString [clazy-unused-non-trivial-variable]

While the concept of building pages/design in QML is very cool and straightforward, bring life to it is a horrible complicated task, sadly.

Fehler beim Erstellen/Deployment des Projekts pythonsample (Kit: SailfishOS-4.4.0.58-aarch64 (in Sailfish SDK Build Engine))
Bei der Ausführung von Schritt “qmake”

That Pyton-Example doesnt work, the Compiler is complaining about “Errors during execution of qmake”.

How does the live-edit on the device works?

Hmmm. That example works from my sdk. Also 4.4.0.58. If you get qmake errors, you can try to ‘clean’ the build and run qmake again:

  • Build Menu → Clean Project "Name of Project
  • Build Menu → Run qmake

You could also try an example like:

This is a WebView thing originally @flypig to demonstrate working between a WebView and QML elements via javascript. My extension adds an http server in Python.

Not sure what you mean? Do you mean with a debugger?

It’s also possible to develope (edit python, qml) directly on the device.

^THIS ^

Well, works for me. I was kind of assuming that title is a QString but it can actually be anything convertible to QVariant. In the end, this method needs to be invoked.

Well, you navigate to the your app directory and edit :slight_smile: I have, in Imageworks, a subdir of my qml dir containing a python file (/usr/share/appname/qml/py/name.py) that can be edited on the phone directly. Restart the app, there you go. You can actually do many fancy things (attach the SDKs debugger to a running app instance) but that’s a bunch of reading.