So i had another look at the code (had to run after i sent previous reply so only just got back to looking at it).. I was already using the QDeclarativeExtensionPlugin. I just don't seem to be able to pin down the syntax. Here is what i have so far.
qmldir
plugin testplugin lib
plugin testplugin lib
To copy to clipboard, switch view to plain text mode
testplugin.pro
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR = lib
OBJECTS_DIR = tmp
MOC_DIR = tmp
HEADERS += conversions.h \
testplugin.h
SOURCES += conversions.cpp \
testplugin.cpp
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR = lib
OBJECTS_DIR = tmp
MOC_DIR = tmp
HEADERS += conversions.h \
testplugin.h
SOURCES += conversions.cpp \
testplugin.cpp
To copy to clipboard, switch view to plain text mode
conversions.h
#ifndef CONVERSIONS_H
#define CONVERSIONS_H
#include <QDeclarativeItem>
class Conversions : public QDeclarativeItem
{
Q_OBJECT
public:
Conversions(QDeclarativeItem *parent = 0);
Q_INVOKABLE int time_ms_to_sec(int val);
};
#endif
#ifndef CONVERSIONS_H
#define CONVERSIONS_H
#include <QDeclarativeItem>
class Conversions : public QDeclarativeItem
{
Q_OBJECT
public:
Conversions(QDeclarativeItem *parent = 0);
Q_INVOKABLE int time_ms_to_sec(int val);
};
#endif
To copy to clipboard, switch view to plain text mode
testplugin.h
#ifndef TESTPLUGIN_H
#define TESTPLUGIN_H
#include <QDeclarativeExtensionPlugin>
class TestPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
void registerTypes(const char *uri);
};
#endif
#ifndef TESTPLUGIN_H
#define TESTPLUGIN_H
#include <QDeclarativeExtensionPlugin>
class TestPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
void registerTypes(const char *uri);
};
#endif
To copy to clipboard, switch view to plain text mode
conversions.cpp
#include "conversions.h"
Conversions::Conversions(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
// need to disable this flag to draw inside a QDeclarativeItem
}
int Conversions::time_ms_to_sec(int val = -1) {
if(val == -1) {
return -1;
}
else {
return (val / 1000);
}
}
#include "conversions.h"
Conversions::Conversions(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
// need to disable this flag to draw inside a QDeclarativeItem
setFlag(QGraphicsItem::ItemHasNoContents, false);
}
int Conversions::time_ms_to_sec(int val = -1) {
if(val == -1) {
return -1;
}
else {
return (val / 1000);
}
}
To copy to clipboard, switch view to plain text mode
testplugin.cpp
#include "testplugin.h"
#include "conversions.h"
#include <qdeclarative.h>
void TestPlugin::registerTypes(const char *uri)
{
qmlRegisterType<Conversions>(uri, 1, 0, "Conversions");
}
Q_EXPORT_PLUGIN2(testplugin, TestPlugin);
#include "testplugin.h"
#include "conversions.h"
#include <qdeclarative.h>
void TestPlugin::registerTypes(const char *uri)
{
qmlRegisterType<Conversions>(uri, 1, 0, "Conversions");
}
Q_EXPORT_PLUGIN2(testplugin, TestPlugin);
To copy to clipboard, switch view to plain text mode
And here is the QML -> I run this QML directly from QtCreater viewer. I have placed comments on line that is failing which shows what im trying to achieve.
Main.qml
import QtQuick 1.0
import TestPlugin 1.0
Item {
width: 100; height: 100
Conversions {
id: convv
Component.onCompleted: {
console.log("Test 1: " + convv.time_ms_to_sec(1000)); //Works ok
console.log("Test 2: " + time_ms_to_sec(2000)); //Works ok
}
}
MouseArea {
anchors.fill: parent;
onClicked: {
console.log("Test 3: " + convv.time_ms_to_sec(3000)); //Works ok
console.log("Test 4: " + Conversions.time_ms_to_sec(4000)); //DOES NOT WORK
}
}
}
import QtQuick 1.0
import TestPlugin 1.0
Item {
width: 100; height: 100
Conversions {
id: convv
Component.onCompleted: {
console.log("Test 1: " + convv.time_ms_to_sec(1000)); //Works ok
console.log("Test 2: " + time_ms_to_sec(2000)); //Works ok
}
}
MouseArea {
anchors.fill: parent;
onClicked: {
console.log("Test 3: " + convv.time_ms_to_sec(3000)); //Works ok
console.log("Test 4: " + Conversions.time_ms_to_sec(4000)); //DOES NOT WORK
}
}
}
To copy to clipboard, switch view to plain text mode
My final goal being..
Main.qml
import QtQuick 1.0
import TestPlugin 1.0
Item {
width: 100; height: 100
MouseArea {
anchors.fill: parent;
onClicked: {
console.log("Test 4: " + Conversions.time_ms_to_sec(4000)); //DOES NOT WORK
}
}
}
import QtQuick 1.0
import TestPlugin 1.0
Item {
width: 100; height: 100
MouseArea {
anchors.fill: parent;
onClicked: {
console.log("Test 4: " + Conversions.time_ms_to_sec(4000)); //DOES NOT WORK
}
}
}
To copy to clipboard, switch view to plain text mode
I have seen references to "setContextProperty" but not sure if this is required for what im after, or if so how to use it.
Thanks again,
Rob.
Bookmarks