Localization in QtQuick from top to bottom - qt

After several weeks of on-and-off research, I still haven't found a thorough guide on how to perform translation/localization in QtQuick (as in, using the QML language, not C++ or Python).
In general, I'm asking what are the steps to properly localize a project as much in QtQuick as possible, with minimal or preferably no C++.
More specifically, there are a good number of holes I need to fill in my understanding of how QtQuick handles localization.
So far, I've:
Appended QT_TR_NOOP() to all of my translatable strings for translation at runtime
Added my file containing all strings to my .pro file using lupdate_only{SOURCES += LanguageStrings.qml}
Generated translation files using QtLinguist
However, I intend to implement an option for dynamically changing the language, and the only example I've seen regarding translation which wasn't entirely in C++ essentially created an instance of the project for each language, rather than changing the strings at runtime.
So, how do I change the language at runtime? Is there a variable I can set? Is it pulled from system locale? I haven't seen a solid answer on this.
Any ideas?

You can do this with minimal C++ (at least I think this is minimal). I've done this in the past using the locale of the system the app is installed on like this (directly in main()):
QGuiApplication app(argc, argv);
QTranslator translator;
if(translator.load(":/translations/myapp_" + QLocale::system().name())) {
app.installTranslator(&translator);
} else {
qDebug() << "Unable to load translation";
}
The translations need to be in the resource system for the above to work. You can of course trigger the above based on user input from QML (e.g. in the settings of your app) at run-time. Here is some example code to do this (https://wiki.qt.io/How_to_do_dynamic_translation_in_QML). I am not aware of a QML-only way to do this.
I tried something else which works, too. You can have your UI in a Loader Element and simply use the setSource functions of that element after the translator changed. I quickly put together a small example, which also includes an example of how to change UI elements outside the Loader, if that is needed (https://github.com/Conntac/qtExamples).

Related

QML Module not found with registered types

I have the following Issue:
In main.qml I get these errors. Although I can use these types perfectly in the code. It looks like it is just an intellisense issue.
These types are registered in main.cpp:
Thse classes are defined in the include folder:
My folder structure looks like this:
Do I have to modify QML_IMPORT_PATH in the pro file? I added src and include folder but it does not work:
QML_IMPORT_PATH += src
QML_IMPORT_PATH += include
The code itself runs fine. It is just an Intellisense issue.
I assume this is simply a Qt Creator bug. Take a look at this one. qmlRegisterSingletonInstance was added to the Qt library in version Qt 5.14. Even though Qt Creator 4.13.3 was built with Qt 5.15.2, the QML code model it uses has apparently still not been updated.
You need to run this code. QtCreator is notorious for flagging errors that don't exist or won't exist. It flags header files for .ui files because you haven't run a build yet so they haven't been generated. Many developers paint their UI files then do a fake build just to generate those files so QtCreator shuts up.
The other thing you need to do is provide the full source code for one of those classes. (I will assume they all have the same issue.)
The example Qt gives here isn't a good one. You should never be able to "new" a Singleton. The constructor should be protected and the Instance() method should construct one and only one if the internal pointer is null.
Do you actually have a method named get() in each of those classes? Does it actually return a pointer? Many return a reference, that is why I'm asking. If we overlook the glaring error of being able to "new" a Singleton, there is one good thing in this example.
QScopedPointer<SingletonTypeExample> example(new SingletonTypeExample);
They used a QScopedPointer to the class.
qmlRegisterSingletonInstance("Qt.example.qobjectSingleton", 1, 0, "MyApi", example.get());
Once you actually build you will have all of the MOC information where it needs to be. This may well make QtCreator happy. Honestly, I've stopped looking at what QtCreator flags anymore because there are so many false alarms.

Change and update geometry mesh at runtime

This question and answer, in Oct 2015, implies it is possible to change Qt3D mesh and update it:
Question
I want to use Qt3d in a situation that involves dynamic runtime
changes of geometric data on the application side.
What is the best way to dynamically change a mesh for an entity?
I'd rather do all this on the C++ side, but QMesh doesn't seem to
provide an API for changing the mesh data.
I looked at some other examples of making a custom QAbstractMesh class
and QAbstractMeshFunctor. It looks like I could possibly make a custom
mesh type to do what I need but I have a question. If I keep a
reference to the QMeshDataPtr that I make from the functor, can I
simply modify the mesh data whenever I want and the entities that
reference it will update automatically?
Answer
The API for this has changed a little in 5.6. The geometric data is
now contained in one or more QBuffer objects and is referenced by one
or more QAttributes that describe the data layout in the buffers. The
QAttributes are rendered by adding them to a QGeometryRenderer
component.
You can either update the above objects on the main thread and call
update() or as before you can also use a functor to have the backend
generate the dynamic data.
Now, my question is about calling update(). Exactly what section of Qt3D API is referred to?
There is a test available at Qt installation directory on my Linux machine:
/home/{user}/Qt5.12.6/5.12.6/Src/qt3d/tests/manual/custom-mesh-update-data-cpp/
which I discovered by following this link when searching Google for qt3d mesh update keywords.
The above test is using Qt3DRender::QBuffer API to update mesh data:
void QBuffer::updateData(int offset, const QByteArray &bytes)
Updates the data by replacing it with bytes at offset.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Code looks like this:
Qt3DRender::QBuffer *vertexDataBuffer;
// ...
QByteArray updateData;
// ...
vertexDataBuffer->updateData(pos,updateData);
// ...

How to Interact with a Qt C++ library inside a QML/QtQuick application?

I recently wrote a Qt based library to manage requests to TheMovieDB.org for a professional desktop application.
Not really experienced with QML and QtQuick, I think about migrating this application to QML which seems to be really adapted for.
I began few tutorials with QML...Amazing.
And now, I wonder if I can integrage my wrapper library into a QML application ?
Basically, let the user capture a movie name into a nice QML interface, froward the input parameters to the c++ wrapper, and retrieve the resulting json results + QPixmap object.
Is it something possible ? I found some piece of codes to access QML components from a Qt c++ application but the inverse yet.
Or maybe am I thinking the things the wrong way ?
Have you ever wrote something similar, I mean regarding the interaction mechanism?

Using QValidator in QCoreApplication

I want to use QValidator and its subclasses (QRegExpValidator, QIntValidator) in QCoreApplication, but get the following error: "QValidator: No such file or directory"
If I add in .pro file the following line: "QT += gui" - all works fine, but that is not a solution for me.
Is there any technique to use QValidator in QCoreApplications?
No, this is not possible. QValidator is part of the gui lib. I suppose the Qt devs thought that QValidator makes only sense with Qt's text input classes. I checked again, above statement is still true. However, I looked into the code, QValidator does not depend on any gui stuff. On first glance it seems to be rather stand-alone. So you might be able to copy qvalidator.cpp and qvalidator.h from the Qt sources into your sources, do a reasonable amount of adjusting, and integrate this into your code. It might be possible, I don't say it will be easy.

generating GUI for C++ code

i need to generate a GUI for a ready code written with C++ the code is divided into some classes containing one that represent user interface to facilitate generating GUI without big modifications on my code
and i already designed the GUI window using QT Designer
now i want to link both logical part (my classes)and GUI part(QT Designer output class) ,how to add all classes to the GUI,how to handle signals coming from GUI and send the appropriate input to the logical part
GUI
get some words from user
get slider input as an int
add files from HDD (logical part need full paths)
out some text
NOTE:first time with QT
thanks in advance for any help
You might want to start with these simple tutorials
http://www.qttutorial.com/qt/hello-qt-your-first-application/
http://www.qttutorial.com/qt/hello-qt-your-first-qt-application-part-2/
I you need a really simple solution - call this in desired order:
QInputDialog::getText();
QInputDialog::getInt();
QFileDialog::getOpenFileName();
QMessageBox::information();
QtAssistant should give you much more details and examples.

Resources