How to use a QML plugin - qt

Can any one say the step by step method to incorporate already created plugins into a new QML based program.
I've got this plugin called qmltermwidget from git qmltermwidget github
Now i have compiled it and I can test the example program in it but I don't know how to use it in my custom application using Qt/QML

See "Creating C++ Plugins for QML".
In your case, you already have a plugin (from your previous question), but the steps involve:
Write a project file for the plugin
Create a qmldir file to describe the plugin
QML extension plugins are for either application-specific or library-like plugins.
Library plugins should limit themselves to registering types, as any manipulation of the engine's root context may cause conflicts or other issues in the library user's code.
The "Module Definition qmldir Files" is where you declare a plugin to be made available by the module.
plugin <Name> [<Path>]
<Name> is the plugin library name. This is usually not the same as the file name of the plugin binary, which is platform dependent; e.g. the library MyAppTypes would produce libMyAppTypes.so on Linux and MyAppTypes.dll on Windows.
<Path> (optional) specifies either:
an absolute path to the directory containing the plugin file, or
a relative path from the directory containing the qmldir file to the directory containing the plugin file.
By default the engine searches for the plugin library in the directory that contains the qmldir file.
The plugin search path can be queried with QQmlEngine::pluginPathList() and modified using QQmlEngine::addPluginPath().

Related

Qt way to get libraries path

I have several QPlugins (shared objects) that I install {CMAKE_PREFIX}/lib/<appname>, how can I find out this path at runtime to load the plugins? Note that the CMAKE_PREFIX user user settable.
If these plugins are part of your application project and you are looking for a build time answer, you can generate a config.h file containing that path.
You can use CMake's configure_file command for that.

How do I localize a Qt qmldir plugin?

My app has a GUI written in Qt QML and we've successfully internationalized it using qsTr() and Qt .ts files. Now we're adding third-party plugin support via Qt's qmldir API. In the .qml files of the plugin, qsTr() works correctly only if the translation is already in the host application's .ts files. How can a third-party QML author add localized strings to their qmldir plugin?
I created a Translator class to organize and load translations. I have a QVariantMap that holds language name and file name, then when I create my Translator I have to provide the source directory since that is a requirement to load translations. My class also takes care of storing in QSettings last language used.
Now, to answer your question, you can always translate the exact same way as if it was a regular application, on your project file you will have to add something like this:
1 - List all your possible translations
TRANSLATIONS = \
translation_sp.ts \
translation_fr.ts
2 - Run lupdate to generate the actual translation files
lupdate project.pro
3 - Run lrelease
Translate with Linguist and create the actual translations. In this example, this step will generate translation_sp.qm and translation_fr.qm
4 - Deploy translations with your plugin
Once you have the .qm files deploy them with your plugin, ideally you could standardize the naming, maybe always using plugin_XX.qm
5 - Load plugin translation in application
To do this you will have to know the path to the translation file and the filename, so if your plugin is installed in the Qt default directories and you standardized the translation filenames this should be simple
qTranslator.load("plugin_XX.qm", "PATH_TO_TRANSLATION_FILE")
app->installTranslator(qtTranslator);
I simplified this in my class. Here are the header and source files of my Translator class, if you want to take a look.
And here's how you use it:
Translator translator(app.data(), "PATH_TO_TRANSLATION");
translator.addTranslation("SP", "plugin_XX.qm");
engine.rootContext()->setContextProperty("translator", &translator);
You can install multiple translators, so it will work for your application and your plugins. When I was writing my class I was not expecting different sources but you can modify it so that everytime you add a translation it will take a 2 letter code for the language, the filename and the path to it.
Hope this helps!

Deploy QML application with localstorage plugin

I have deployed a QML application (static build on windows, following this how-to: http://qt-project.org/wiki/How-to-build-a-static-Qt-for-Windows-MinGW). However, the qml_import_trace (screenshot below) reveals that LocalStorage is loaded from the Qt/Static folder on the development computer, not from the release folder. Hence, when launched at another computer, the LocalStorage module is not found. How may the LocalStorage plugin/module be shipped with the application?
Including the following lines in the .pro files will give svg support. Am I only missing a qtplugin for sql/localstorage? In that case, what is the proper plugin name? Also, where can I find valid inputs for QTPLUGIN+= and QT+= ?
QTPLUGIN += qsvg
QT += svg sql
If I understand you correctly you want to copy the needed files to the release folder automatically.
Use the windeployqt.exe (in qt/bin folder) with --qmldir option. It will scan the given path for QML files and collect the QML components imported in those files.
A solution, although not optimal, was to manually copy the QtQuick/LocalStorage folder from the static folder into the release folder

Plugin is not recognized in QML Desktop Appication Deployment

I have wrote a Qt Quick Desktop application in c++ qnd Qt Creator(QML) on Windows7. Now I have to deploy it.
I'm using Qt Quick Desktop Components plugin in my application, I've installed it according to these instructions, and I'm using it with:"import Qt.labs.components", as written there.
I tried adding to the .pro file:
QML_IMPORT_PATH = C:\QtSDK\Desktop\Qt\4.7.4\mingw\imports\Qt\labs\components
but I saw it's working well without it, and I removed it.
I've read a guide how to deploy such an application here, and followed it; I have now a deployment folder, with: the .exe file, the needed dll's, and a folder hierarchy like:Qt/labs/components.
in components I put the styleplugin.dll(for desktop components), and a qmldir file, with the content: plugin styleplugin, excactly like in the doc.
but when I'm runnig my application.exe from the deployment folder in another computer, I'm getting a white, empty window, means: It didn't find the .dll file.
Should you explain me please what's wrong?
I know two reasons, when app can not load plugin dll:
Some of dependecies of the plugin dll are missing or can not be found and that is why it can not be loaded. Qt Creator or Visual Studio environment can be different than the system one. For example, your IDE can modify PATH environment variable. Check plugin's dependencies availability with Microsoft Dependency Walker tool in the same environment where you launch your app.
App can not find plugin in standard directories. To check this you should specify plugin import directory explicitly:
QDeclarativeView *rootView = new QDeclarativeView()
rootView->engine()->addImportPath(QLatin1String("path/to/your/imports"));

Qt including resource directory structure inside executable

I'm using QWebView to run a web app. There are 650+ files. Placing the web app's directory in the source directory does not result in the executable bundling the directory.
How do I include the entire web app directory so that the executable will be able to render the files.
Note: I have currently added index.html as a resource, and can access it with qrc:// - But since I cannot add the entire directory structure to a qrc (can I?), the executable does not include the other files.
You need to put an XML node into the .qrc file for each file you want to use using the Qt resource system.
This can be done using a simple pre-build script. Take a look at qrcgen. Quoting the blog post behind this link:
The script I created, qrcgen, takes a directory and a prefix, recursively scans the directory and generates a .qrc file with the same name as the directory scanned. It has solved my problem, and I hope it can help others. It is also available via PyPI, just "easy_install qrcgen".
In order to update the .qrc file whenever your directory contens change, you need to include this step into your build process:
For C++/Qt projects, you can add this step in the build configuration in QtCreator or add in your qmake file a system(...) statement. Note that such commands aren't portable in general. (If it's not portable, you can put some operating system conditions around multiple commands.)
For PyQt/PySide projects, I don't know how to do this, but I'm sure you find a solution for this too.

Resources