Qt linguist lupdate ignores qml files - qt

When running lupdate none of the qsTr in the qml files are recognized. The resulting .ts file doesn't contain any translation context.
$ lupdate -verbose App.pro
Updating 'translations/en.ts'...
Found 0 source text(s) (0 new and 0 already existing)
The project should be set up correctly:
OTHER_FILES += \
content/main.qml
TRANSLATIONS += \
translations/en.ts
In the main.qml among other things:
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Open")
onTriggered: Qt.quit();
}
}
Menu {
title: qsTr("...")
MenuItem {
text: qsTr("About")
onTriggered: {
aboutApplicationDialog.open()
}
}
}
}

You can generate the translation file by running lupdate on QML file :
lupdate main.qml -ts main.ts
To get the .ts file by running lupdate on the project .pro file you can use a workaround. From the Qt documentation :
The lupdate tool extracts user interface strings from your
application. lupdate reads your application's .pro file to identify
which source files contain texts to be translated. This means your
source files must be listed in the SOURCES or HEADERS entry in the
.pro file. If your files are not listed the texts in them will not be
found.
However, the SOURCES variable is intended for C++ source files. If you
list QML or JavaScript source files there, the compiler tries to build
them as though they are C++ files. As a workaround, you can use an
lupdate_only{...} conditional statement so the lupdate tool sees the
.qml files but the C++ compiler ignores them.
If you specify your .qml files in the application like :
lupdate_only{
SOURCES = content/main.qml
}
When you run lupdate on the project .pro, the resulting .ts file will contain the QML translation context.
Note that you must stick to the brace style shown, using an alternate style such as e.g.
# DON'T USE, FAILS!
lupdate_only
{
SOURCES = content/main.qml
}
fails (at least on OS X 10.12 / Qt 5.7) with large amounts of compiler warnings and errors that are far from giving any hint at the actual problem, e.g.
clang: warning: <qml source file>: 'linker' input unused
clang: warning: argument unused during compilation: '-g'
clang: warning: argument unused during compilation: '-isysroot /Applications/Xcode_7.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk'
...
clang: error: no such file or directory: 'Page1.o'
clang: error: no such file or directory: 'Page1Form.ui.o'
Alternately, you can use a continuation character:
lupdate_only \
{
SOURCES = content/main.qml
}

As of Qt 5.8.0 no tricks are required in the .pro file anymore.
QML files can be listed once inside a .qrc Resource Container, and they will be picked up correctly by:
compiler
application at run-time
lupdate for translation
.pro file:
RESOURCES += application.qrc
.qrc is an XML file, usually managed via qtcreator without looking at its contents. More info about qrc files can be found here:
http://doc.qt.io/qt-5/qtquick-deployment.html#managing-resource-files-with-the-qt-resource-system
.qrc support was added to lupdate on 2016-10-25:
http://code.qt.io/cgit/qt/qttools.git/commit/?id=f2ebd51d96ad49eb826a4e37e67d506fffcbd40c
It didn't make it into Qt 5.7.1 release, but it will be available in 5.7.2 (if there ever is one).

Using lupdate_only seems like a viable solution.
However note that QtCreator will also pickup the qml files as source files, so now all qml files are listed duplicate when you navigate the project.
To avoid this I used a different approach where I update the translations through a shell script:
#!/bin/bash
../../5.5/gcc_64/bin/lupdate *.pro
for tr in *.ts
do
../../5.5/gvv_64/bin/lupdate *.qml -ts $ts
done

Related

QT QML resource files do not recompile after changes

I'm using QT 5.9.1 working on Mac OS. My project is mobile App with C++ logic and QML UI Layer. All QML files are included into qml.qrc file, so in my .pro file I have
RESOURCES += qml.qrc
Inside qml.qrc there is a list of all resource files I use in Project, such as pictures, icons and QML files, in QT Creator it's displayed OK:
As you can see some QML files are located in ROOT path of qml.qrc when other files are in subfolders , e.g. "qrc:/Elements/".
So problem is that whenether I make changes in Files that located in root of qml.qrc - they are normally recompiled when I press build, rebuild, or clean and build, so I can see my changes. As a result in my build directory I see that qml_qrc.cpp (as I understand this file contains cpp representation of my resource files and is used to compile them) file is refreshed,my changes are applied and everything is OK.
Here is just piece of this file, which begins with cpp hex representation of MainPage.qml resource file.
/****************************************************************************
** Resource object code
**
** Created by: The Resource Compiler for Qt version 5.9.1
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
static const unsigned char qt_resource_data[] = { //
/Users/admin/QtProjects/LazuritApp-temp/MainPage.qml
0x0,0x0,0x7,0xd1, 0x0,
0x0,0x1e,0xbb,0x78,0x9c,0xd5,0x19,0x5d,0x6f,0xdc,0x36,0xf2,0x7d,0x7f,0x5,0x6f,
0xb,0x14,0xbb,0x69,0xa2,0xf5,0xae,0xed,0xa4,0xde,0xa0,0x77,0xf0,0xba,0x49,0x63,
0x20,0x45,0xda,0xda,0x68,0x1e,0xe,0x45,0xc1,0x95,0xa8,0x15,0x2f,0x5a,0x51,0xa5,
0x24,0xdb,0x5b,0xc3,0x40,0xda,0x2,0xed,0x1,0x79,0x28,0x70,0x68,0x71,0xcf,0xf7,
0xf,0xdc,0xbb,0x6b,0x1b,0x34,0x4d,0xfa,0x17,0xb4,0xff,0xa8,0x43,0x52,0x5f,0x94,
..............
But if I change those resource files located in subfolders, qml_qrc.cpp file is not refreshed, so my resource files are not rebuild, even if I try Clean, then rebuild. Even if I do "run qmake" manually and then rebuild. The only thing helps in this situation - to manually delete build folder (or to be more precisely you can just delete qml_qrc.cpp file). So then pressing "Build" will create new qml_qrc.cpp file, which will contain correct code, with changes I've done in my resource files.
Can someone help me or explain why this happens and what can I do in this situation? deleting manually and rebuilding is annoying, but placing all resource files in root path of project is also not a good decision...
I also tried to paste
update qml
qml_scenes.depends = $$PWD/QML Files/OrdersPage.qml
qml_scenes.commands =
QMAKE_EXTRA_TARGETS += qml_scenes
as was described here, but it didn't help
ok, after hours of digging in QTBUGS traces, stackoverflow and others forums I found solution, which somehow satisfies me..
1) Create script file (for me it's .sh file as I working on MAS OS, for Windows it will be .bat file) with "touch" command to qml.qrc file. In my case it contains 2 lines :
#!/bin/sh
Touch qml.qrc
2) Add Custom build step (Projects->Build Settings->Build Steps->Add Custom Process Step). Choose your created .sh file, choose working directory when you build is located. Make this custom step to be the first executed (before qmake and Make)
3)So, now changes in qml resource files will be compiled every time you build the project. Script will firstly touch our qml.qrc file, which will refresh it's modified date, so that qml.qrc (hence, our qml resources too) will be added to makefile dependencies.
it seems to be pretty rough way to solve the problem, but at least you don't have to Clean, Rebuild and so on..
If someone have better solution, please let me know)
Unfortunately, the solution given in a link provided in the comments seems to be incomplete and/or broken. But it can be done like this:
update_qml.target = qml.qrc
update_qml.commands = echo>>$${update_qml.target} # same as touch
update_qml.depends = $$files(path/to/resource/files/*, true) # recurse into subdirs
QMAKE_EXTRA_TARGETS += update_qml
PRE_TARGETDEPS += $${update_qml.target}
For those who interested in how it works, QMAKE_EXTRA_TARGETS produces the following Makefile rule:
qml.qrc: file1 file2 ...
echo>>qml.qrc
So issuing a command make qml.qrc will update the timestamp of qml.qrc as needed. However, to save us from manual typing it every time, we also make use of PRE_TARGETDEPS which fixes the main building rule:
$(DESTDIR_TARGET): qml.qrc $(OBJECTS) and other stuff ...
$(LINKER) flags objects and other stuff ...
So now make utility will scan the previous rule every time while building the target.

Qt lupdate in .pro file issue

I had to mark some of the .xml files for internationalization. I do not use lupdate manually from cmd, instead I put it in the project's .pro file like:
lupdate_only{
SOURCES += $$EXTRA_XML
}
The above code works just fine, but as you noticed I had to put the xml files in SOURCES. As a consequence the .xml files appear in the Sources virtual folder from the left Projects' perspective window, just next to the .cpp files. I find this solution a bit nasty and confusing.
- Project
- - Headers
- - Sources
- - - main.cpp
- - - some.xml //not wanted here
Is there a way to use lupdate, in .pro, on different files such that those files won't appear in the Sources folder? Thanks!
UPDATE
I use Qt Creator 4.0.3
lupdate_only {
SOURCES += $$EXTRA_XML
}
With this conditional statement, lupdate tool sees the .qml files but qmake will ignore it.
I found the solution to my problem, however I think it's a Qt Creator bug. I just moved the lupdate statement with its contents into a .pri file and now the xml files do not appear under the Sources virtual folder. (the .pri file is included in .pro)

Error: undefined reference to QQuickRectangle::QQuickRectangle(QQuickItem*)

I want to use Qt Quick 2 directly from C++ without those qml/javascript stuffs that could slows down the app, but i got compilation error when using QQuickRectangle (Rectangle when used from qml).
In .pro file i have: QT += quick quick-private core-private gui-private declarative-private qml-private
and in .cpp file - #include <private/qquickrectangle_p.h>
I compiled the Qt 5.5 from sources and the command nm -D /usr/local/Qt-5.5.0/lib/libQt5Quick.so.5 | grep "Rectangle"
doesn't print anything related to QQuickRectangle, so it looks that it is not included in the shared library, but i don't know why, because the qquickrectangle.cpp exists in qt sources, so it should be included in the shared library after compilation.

Qt Error: cannot open include file: 'opencv2/core/core.hpp' no such file or directory

I want to build an opencv project using Qt 5.1_VS2010. I have included the path for opencv include files. But when I run the code, I get the error that specific .hpp file does not exist! I have checked in the directory and it actually exists there! another point that when I was trying to include it in my main c++ file, the auto completion guide actually helped me complete the whole path! so it means the directory and the header file was already recognized! anyway, part of my .pro file that includes the path is like this:
INCLUDEPATH += "C:/opencv/build/include"

Qt: mingw compiled library does only work with both library.so and library.lib file present

I compiled a library using the MinGW toolchain provided with Qt 5.0.2 on Windows. As a result I received a library.so file. First I failed using the library in a Qt application, but now I found out that everything works fine when I make a copy of the liblibrary.so file and call it liblibrary.dll or liblibrary.lib (which is the only file ending supported by the add library wizard in QtCreator).
Now I wonder if this is normal or if I should change something in order not to have both files (which are exact copies). Leaving one away makes the application crash during start up. I added the library as follows to my Qt pro file:
LIBS += -L"../path/to/library" -llibrary
INCLUDEPATH += $$quote(../path/to/library)
EDIT: I compiled the library using the MinGW of Qt, not as Qt project but using mingw32-make and the provided Makefile. As a result I get the liblibrary.so.
EDIT: It seems to work also when renaming the copy to liblibrary.dll instead of .lib. But still, I need two files to make the application work -- the .so and the .dll.
Chris
That's weird, I think you should get a *.a and *.dll files when building a shared lib with MinGW on Windows, as said in the documentation:
In windows, MinGW will output .a and .dll, MSVC2010 will ouput .lib and .dll. In linux, MinGW will output .so, .so.1, .so.1.0 and .so.1.0.0 – .lib, .a and .so are import libraries.
You definitely shouldn't rename your file!
Be careful to:
not to include the "lib" prefix after "-l" in your project file.
put everything after after "-l" in lower case as you're on Windows
not adding any extension to your library name after "-l"
add and reference the .h file used in your library
A real example using QtWebsocket lib:
INCLUDEPATH += "$${PWD}/include/"
LIBS += -L"$${PWD}/libs/" -lqtwebsocket
...
HEADERS += ... \
$${PWD}/include/QWsSocket.h \
...
In my include/ folder, I have the following file:
QWsSocket.h (taken from original project - required)
In my libs/ folder, I have the following file:
libQtWebsocket.a
QtWebsocket.dll
Edit: I struggled with this too initially. Have you tried to build your lib as a static lib instead (CONFIG += staticlib in your library project)? This might help you getting you *.pro file right before switching to using the shared library.
Edit 2: Ok, the fact that you get a *.so file is still a bit odd. In this question
the user has the same issue as you and keep both files, which is just a workaround. According to a later answer it seems that you need to modify your makefile to generate a file with the proper extension. Maybe this will help: http://www.mingw.org/wiki/sampleDLL

Resources