Show application version on my "AboutPage"

I would like to show my application version automatically on my AboutPage.qml as Text.

How can I read or grab the version number from yaml or spec file, or is there another way?

It’d be nice to have, saves updating that one aspect of the app manually.

TIA. . .

I have it like this:

  1. In .pro file: DEFINES += MYAPP_VERSION='\\"$$VERSION\\"'
  2. In main(): app->setApplicationVersion(QStringLiteral(MYAPP_VERSION));
  3. In your QML: Qt.application.version

This is a slightly improved version of what i got contributed from Rudi T, kudos to him.

3 Likes

Thanks @attah I get en error for number 2.

use of undeclared identifier 'app'

Hmm, yes… too much of an oversimplification.
You might need to do the “complicated” version of the app launch…
See here:

1 Like

Or, you use something like sed -i "s/unreleased/%{version}/" qml/pages/AboutPage.qml in the .spec file in the %build section, while having “unreleased” as the version string in the source.

1 Like

Maybe a “cheating” way of doing it, and not quite correct… But if you translate your apps you could have the version number in your .ts file :stuck_out_tongue:

Presumably @Edz, like me, is too absent-minded to remember to update the version in multiple places, regardless off which places those are. I don’t think putting it in the .ts helps then, or?

Would not the original language be a fallback when the string is not translated in $whatever

If it’s done like this, does it mean that the version number in qml-pages is updated only if the main.cpp is recompiled? Then if there’s changes in qml-files only, main() would be using APP_VERSION that was valid when main.cpp was compiled last time.

True. But in practice that has not been a problem. When building for release you need to clean up between building for different architectures anyway. And when testing, at least i am not so fussed about having perfect temporary versions.

Thanks for all the input guys, not that I am able to get any of it working. I actually thought going into the idea, that it would be fairly easy to implement…snort!

I mainly played with the code shown by @attah, but have come to the conclusion of “I can no longer be bothered with this idea”.

Perhaps I’ll revisit at some point, but thanks again for input. I do have vague recollection the coderus had made an app with “automagic” for displaying application details in an about page including the version number.

It turns out that in addition to what @attah wrote above, it also takes adding the following to the .spec file:

VERSION=%{version}

as in https://github.com/attah/harbour-seaprint/blob/master/rpm/harbour-seaprint.spec

Then it works OK. But it is annoying like hell, because everytime the spec file gets automatically updated it obviously gets removed.

Anyone knows if/how it can be added to the .yaml file instead, so that it just stays there?

Put it in the perstistent sections in the spec, something like

# >> build pre
    export CFLAGS="-DVERSION=%{version}"
# << build pre

Or depending on your configurer use something like CMAKE_EXTRA_CFLAGS or the qmake equivalent.

Maybe:
Version: %{version}
will work? It should generate VERSION=… in spec

Yes:

QMakeOptions:
    - VERSION=%{version}

All available directives are listed here https://github.com/sailfishos/spectacle.

3 Likes

Yes, yes, yes! Thanks a lot, @martyone!

Fantastic, thank you!

Well spotted, and sorry for the confusion!

@nephros That might indeed be even better.

Just to add one more hint, if you have spectacle installed, the README is also available locally at /usr/share/doc/spectacle-0.32/README.md

Here’s how I do it in my apps. By building a temporary header file that is included in main.cpp I make sure the version number stays up-to-date even without actual code changes (cf. above).

  1. Define version in yaml and set qmake options (make sure you have Builder: qmake5)
  1. Use this qmake option in pro to pass it to cpp
  1. Expose the values from cpp to qml (or use attah’s way)
  1. Finally use it in AboutPage.qml

By including the helper pri file in the pro file my temporary header file gets updated when qmake is run.

2 Likes

Adding it as QMakeOptions in .yaml file as @martyone suggested works perfectly fine and is persistent. I already changed all my projects to use it this way, finally just one place (Version in .yaml) to change the version string, and it is then seen everywhere else, both in C++ and QML.

P.S. What happened to the “Accept as solution” button?