Hi anda_skoa:
I assume you suggest something similar to plugin approach, use c++ to initialize, and import the plugin to qml doc.
I tried this approach, and successfully build the following two files and put these two files in /usr/lib/qt5/qml/GuiTestInit/ folder:
libqmlguitestplugin.so
qmldir
The plugin.cpp is stub for now:
#include <QDebug>
#include <QtQuick>
#include <QtQml/qqml.h>
#include <QtQml/QQmlExtensionPlugin>
#include <QGuiApplication>
#include <QQuickItem>
#include <QQuickWindow>
class GuiInit: public QObject
{
Q_OBJECT
private:
GuiInit( QObject* parent = NULL );
static GuiInit* db_inst;
// DatabaseThread* m_database_thread;
QList<QObject*> initialized_databases;
public:
static GuiInit* instance( QObject* parent = NULL );
~GuiInit( void );
void initAll();
};
class GuiTestInitModel : public QObject
{
Q_OBJECT
public:
GuiTestInitModel(QObject *parent=0) : QObject(parent)
{
}
~GuiTestInitModel()
{
}
Q_INVOKABLE int init(const QString &msg)
{
return 0;
}
public:
};
class GuiTestInitPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA( IID "org.qt-project.Qt.QQmlExtensionPlugin" FILE "qmlguitestinit.json" );
public:
void registerTypes(const char *uri)
{
qmlRegisterType<GuiTestInitModel>(uri, 1, 0, "GuiTestInit");
}
Q_INVOKABLE void databaseInit( void )
{
}
};
#include "plugin.moc"
When testing using a simple qml:
import QtQuick 2.3
import QtTest 1.0
import GuiTestInit 1.0
Rectangle {
id: backgroundRec
visible: true
width: 1024
height: 600
Text
{
text: qsTr("Hello World")
anchors.centerIn: parent
font.family: "Helvetica"
font.pointSize: 24
}
Rectangle
{
id: testButton
width: 224
height: 60
anchors.top: parent.top
anchors.topMargin: 100
anchors.left: parent.left
anchors.leftMargin: 400
Text
{
id: backText
width: paintedWidth
height: parent.height
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
color: "black"
text: qsTr( "Test" )
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pointSize: 20
MouseArea
{
anchors.fill: parent
onPressed: { backText.opacity = 0.3 }
onReleased: { backText.opacity = 1.0 }
}
}
}
TestCase {
name: "StartupTest"
function test_backgroundRec() {
compare(backgroundRec.width, 1024)
compare(backgroundRec.height, 600)
compare(backgroundRec.visible, true)
}
function test_button() {
compare(testButton.width, 224)
compare(testButton.height, 60)
}
}
}
When running the test application, I got the following error:
./tst_calculatedCriticalRangesFormTest_opti -platform eglfs
file:///home/root/gui_tests/calculatedCriticalRangesFormTest/tst_calculatedCriticalRangesFormTest.qml:12:1: plugin cannot be loaded for module "GuiTestInit": Cannot load library /usr/lib/qt5/qml/GuiTestInit/libqmlguitestplugin.so: (/usr/lib/qt5/qml/GuiTestInit/libqmlguitestplugin.so: undefined symbol: _ZN7GuiInitD1Ev)
import GuiTestInit 1.0
^
********* Start testing of tst_calculatedCriticalRangesFormTest *********
Config: Using QtTest library 5.3.2, Qt 5.3.2
QWARN : tst_calculatedCriticalRangesFormTest::tst_calculat edCriticalRangesFormTest::compile()
/home/root/gui_tests/calculatedCriticalRangesFormTest/tst_calculatedCriticalRangesFormTest.qml produced 1 error(s):
/home/root/gui_tests/calculatedCriticalRangesFormTest/tst_calculatedCriticalRangesFormTest.qml:12,1: plugin cannot be loaded for module "GuiTestInit": Cannot load library /usr/lib/qt5/qml/GuiTestInit/libqmlguitestplugin.so: (/usr/lib/qt5/qml/GuiTestInit/libqmlguitestplugin.so: undefined symbol: _ZN7GuiInitD1Ev)
Working directory: /home/root/gui_tests/calculatedCriticalRangesFormTest
View: QQuickView, import paths:
'/home/root/gui_tests/calculatedCriticalRangesFormTest'
'/usr/lib/qt5/qml'
Plugin paths:
'.'
FAIL! : tst_calculatedCriticalRangesFormTest::tst_calculat edCriticalRangesFormTest::compile() plugin cannot be loaded for module "GuiTestInit": Cannot load library /usr/lib/qt5/qml/GuiTestInit/libqmlguitestplugin.so: (/usr/lib/qt5/qml/GuiTestInit/libqmlguitestplugin.so: undefined symbol: _ZN7GuiInitD1Ev)
Loc: [/home/root/gui_tests/calculatedCriticalRangesFormTest/tst_calculatedCriticalRangesFormTest.qml(12)]
Totals: 0 passed, 1 failed, 0 skipped
********* Finished testing of tst_calculatedCriticalRangesFormTest *********
Can anyone tell me why plugin load error?
Thanks,
Alex
Bookmarks