The doc shows I can use QML_ELEMENT macro to create QML types from C++ by adding some variables in qmake's .pro file. But I'm using cmake
Update (Qt 6.2+)
As of Qt 6.2, qt_add_qml_module is a single command for building qml modules that should take care of virtually everything, replacing amongst others the old qt6_qml_type_registration command.
Old answer (Qt 6.0/6.1)
Now that Qt 6.0 is out this is supported, albeit poorly documented. What you need now is:
set_target_properties(foo PROPERTIES
QT_QML_MODULE_VERSION 1.0
QT_QML_MODULE_URI Foo
)
qt6_qml_type_registration(foo)
you can then do in qml:
import Foo
and you'll have access to types that have QML_ELEMENT and friends. Notes:
Two files are created in the build output folder, <project>_qmltyperegistrations.cpp and <project>.qmltypes, if your imports are failing you can look at those to see which types are missing. I found that I needed to do full recompiles sometimes after adding/removing registered types.
Qt examples have been migrated to cmake, so take a look at e.g. Examples/Qt-6.0.0/quick/tableview/gameoflife to see it in action
There are now pro2cmake.py and run_pro2cmake.py files in the Qt sources at Qt/6.0.0/Src/qtbase/util/cmake. They are mentioned on this Readme page, you can find them here, haven't tried it myself.
Edit (Qt 6.x)
This answer was originally posted for Qt 5.15. Now that Qt 6 is available, and if you are using Qt 6, refer to the answer from #Adversus.
Original answer (Qt 5.x)
From what I can see, CONFIG += qmltypes, which is required to use QML_ELEMENT, is not yet supported in CMake by looking at the documentation, even for the master branch.
And the efforts to provide a python .pro to cmake converter are for Qt6, not merged, and not functional as far as I can tell, by testing them from util on the wip/cmake branch (the CMakeLists.txt didn't have relevant information).
You can see that the actual conversion script does test for qmltypes presence in CONFIG, but it doesn't seem to map to anything usable for CMake.
Solution
Instead of using QML_ELEMENT and CONFIG += qmltypes, which is brand new from Qt 5.15 (latest when writing this), and not supported at this time with CMake, use the good old qmlRegisterType from C++:
#include "YouCustomCppClass.h"
int main(int argc, char** argv) {
// Let you import it with "import ModuleName 1.0" on the QML side
qmlRegisterType<YouCustomCppClass>("ModuleName", 1, 0, "YourQmlComponent");
//Create your QML view or engine
}
This won't require anything specific on the project file side, as long as your code/plugin executes qmlRegisterType statement.
You can refer to Qt's documentation, same page as yours, but for Qt 5.14 instead of latest, which describes exactly that: Writting QML extensions for C++ | Qt 5.14
Related
I'm currently building a minimalist app following this CMake architecture:
-root
--QmlModule
---Component1.qml
---Component2.qml
--App1
---main.cpp
---main.qml
--App2
---main.cpp
---main.qml
I use "qt6_add_qml_module" to create a QML module at "QmlModule" level as a STATIC library.
qt_add_library(myComponentTarget STATIC)
qt6_add_qml_module(myComponentTarget
URI QmlModule
VERSION 1.0
QML_FILES
Component1.qml
Component2.qml
RESOURCES
logo.png)
Then, at App1 (and App2) level, a link to the module is done using "target_link_libraries". "qt6_add_qml_module" does some work behind the scenes in order to expose the module trough an automatically generated plugin named "your_component_URIplugin". More details about this here.
add_executable(App1Exe
main.cpp)
qt6_add_qml_module(App1Exe
URI App1
VERSION 1.0
QML_FILES
main.qml)
target_link_libraries(App1Exe
PRIVATE
myComponentURIplugin)
At Root level, I overload QML_IMPORT_PATH in order to link to the build folder and add all subdirectories.
set(QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qmlModule)
add_subdirectory(QmlModule)
add_subdirectory(App1)
add_subdirectory(App2)
I run CMake without any errors, and open App1/main.qml file.
On my import QmlModule, the module can't be found:
module "lupinComponentsplugin" is not installed
How to make my module visible from my Apps ?
What step am I missing ?
I'm currently doing something similar.
I have created a demo app where I import modules. The modules provide QML and C++ items to the main app. Check the comments in the CMAKE files to find out how this works.
Here is the link:
https://gitlab.com/basic53/DemoApp
Feel free to comment on this.
Another tip: If qt_add_qml_module is not working properly, sometimes it is necessary to remove the whole build folder and update the QML code model. You can check the generated files to see, if your plugin has been created with all its types.
CMake itself was fine, this was a runtime error and not a link error.
This issue was raised because the QQmlApplicationEngine wasn't finding path towards my module's QMLDIR.
In the end, the only thing missing was an additional import path ":/" in QQmlEngine:
QQmlApplicationEngine engine;
engine.addImportPath(":/");
I'm a beginner in Qt. I have an app designed using Qt which has multiple windows with each consisting of several labels with hard coded text. What would be the easiest way to add multi language support for this app? Which built in Qt objects/functions I should use?
My current idea is to maybe create a separate xml file including the text for all labels for a language. Then once the user selects a language icon from the menu, load the relevant xml file. But I have no idea how to do this. Any help would be highly appreciated!
UPDATE:
I have tried to implement the example given here. But it throws the following error and I can't fix it. 'class Ui::MainWindow' has no member named 'menuLanguage'
QActionGroup* langGroup = new QActionGroup(ui->menuLanguage);
Try Qt Linguist:
I made you a simple example:
.pro
TRANSLATIONS += translation_fa.ts
.h
#include <QTranslator>
QTranslator translator;
.cpp
if(translator.load("E:/Qt/Linguist/linguist/translation_fa.qm"))
qDebug()<<"successfully load qm file.";
else
qDebug()<<"problem in load qm file.";
// change language to second language
qApp->installTranslator(&translator);
// change language to default language
qApp->removeTranslator(&translator);
Don't forget to use Qt Linguist Tools.
This is a sample project for your question on github download here.
Qt has translation support. Look into Qt Linguist
Basically, you mark all you hard coded texts with a call to QObject::tr, e.g.
lbl->setText(tr("My text to translate"));
Qt Linguist parses all source files and UI forms for such calls (using lupdate.exe) and builds a ts file. With the Qt Linguist GUI application, you can translate them. With lrelease you create a qm file that will be loaded at runtime, and automatically translate the texts
Qt has built in functions for managing multiple languages:
see: https://wiki.qt.io/How_to_create_a_multi_language_application
Research lupdate lrelease and linguist
I am using OpenCV 3.0 beta.
I tried to create a face recogniser using createLBPHFaceRecognizer(); class as,
**Ptr <FaceRecognizer> model = createLBPHFaceRecognizer();**
the error I have is
**error: 'createLBPHFaceRecognizer' was not declared in this scope**
I have researched and found that the class exists in contrib module of opencv2
(opencv2/contrib/contrib.hpp) in previous versions of OpenCV
But this module is not available in opencv 3.0 beta.
So where are the recogniser classes defined in opencv 3.0?
If they are not defined,how can we add this module in addition to the existing modules?
you will have to download and build the opencv_contrib repo.
after running cmake, make, make install,
#include <opencv2/face.hpp>
// note the additional namespace:
cv::Ptr <cv::face::FaceRecognizer> model = cv::face::createLBPHFaceRecognizer();
// proceed as usual
from https://github.com/opencv/opencv_contrib:
Start cmake-gui
Select the opencv source code folder and the folder where binaries
will be built (the 2 upper forms of the interface)
Press the configure button. you will see all the opencv build
parameters in the central interface
Browse the parameters and look for the form called
OPENCV_EXTRA_MODULES_PATH (use the search form to focus rapidly on
it)
Complete this OPENCV_EXTRA_MODULES_PATH by the proper pathname to
the /modules value using its browse button.
Press the configure button followed by the generate button (the
first time, you will be asked which makefile style to use)
Build the opencv core with the method you chose (make and make
install if you chose Unix makfile at step 6)
To run, linker flags to contrib modules will need to be added to use them in your code/IDE. For example to use the aruco module, "-lopencv_aruco" flag will be added.
On my Debian installation
$ dpkg -l libopencv-contrib-dev
ii libopencv-contrib-dev:amd64 3.2.0+dfsg-6 amd64 development files for libopencv-contrib3.2
enables me to use contributed modules with just an additional include. For example:
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
auto model = cv::face::createLBPHFaceRecognizer();
Im trying to read from a file and put into to the text edit and it keeps saying QIODevice::read:device not open. The .txt file is in the same location as my .qrc and .cpp file. I was following a step by step guide from online. From my understanding, they changed something when they went from Q4 to Q5. Does anyone have any hint on how I can fix this. thanks
//My findstuff.h
#ifndef FINDSTUFF_H
#define FINDSTUFF_H
#include <QWidget>
namespace Ui {class FindStuff;}
class FindStuff : public QWidget{
Q_OBJECT
public:
explicit FindStuff(QWidget *parent = 0);
~FindStuff();
private slots:
void on_goButton_clicked();
private:
Ui::FindStuff *ui;
void getTextFile();
};
If you're reading from a .qrc resource file you have to run qmake ("Build->Run qmake" in Qt Creator) before it will be available.
You're not passing the absolute path of the file to QFile::open(), and you're not checking the result of opening the file. In your case, it's a failure and open() returns false, but you're ignoring it, instead of fixing the problem (the wrong path) that caused it.
This has zilch to do with Qt 4 -> Qt 5 upgrade, and everything to do with you assuming the wrong thing about the current directory your application happens to find itself with. Generally speaking, the current directory (or working directory) is arbitrary, and platform- and circumstance-specific, and wholly out of your control. Unless the user gives you a filename that's implicitly referenced to the current working directory (e.g. as a relative path given a commandline argument), you must use absolute file paths or things simply won't work.
It can be related to the version of Qt, since Qt5 sometimes doesn't work with MSVC2010.
I have Qt 5.4 and my code gave the same error, when it was working with MSVC2010 OpenGL as a compiler. I manually added MinGW 32bit to use it as compiler and it worked.
P.S. I have not installed MSVC2013 for Qt 5.4., and it works sometimes with MSVC2010 OpenGL without error, but not in this case.
this is mostly the case if you close a file which is not opened - so just remove the close statement for example:
file->close();
just remove it ;)
I had this problem and it turned out Qt Creator hadn't actually added the .qrc file to my project. I'm using Qt Creator 4.1.0 on a Mac and the projects view doesn't always get populated after creating a new project, first requiring a restart of Creator. My version of this problem may relate to that.
It does not have anything to do with Qt version.
Even though your .txt file is in the same directory as your .cpp file, you still need to add the directory. I had the same problem and that simple solution worked well.
Arman Arefi
VTK offers the possibility to extend the library with C++ classes so that they can be used in Python (or in any language supported). As far as I know, there are two way to do that, using the examples folders vtkLocal or vtkMy provided with the library (Examples/Build/).
I would like to use this functionnality, but I don't understand the installation process (vtk 5.8 on Debian). I follow the README instruction on vtkMy, it compiles and generetate 3 files : vtk*.so vtk*Python.so and vtk*PythonD.so.
What I am suppose to do next ? I've tried appending those file's path to $PATH, $PYTHONPATH or $LD_LIBRARY_PATH as suggested, but I've never been able to import anything into Python.
Any insight or even some instructions on how to compile/import/use the dummy classes provided in vtkMy would be a tremendous help.