Results 1 to 8 of 8

Thread: Qt import library - creating a QML function

  1. #1
    Join Date
    Apr 2015
    Location
    West Sussex, England
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Qt import library - creating a QML function

    Hi All,

    Im probably being really dumb and staring right at the issue but can't see the solution.. I've written an import library that we use for various Qt 4.8 QML applications. I have various objects that i can instantiate inside QML without any problem e.g.

    Qt Code:
    1. Logger {
    2. id: write;
    3. name: "Main.qml";
    4. Component.onCompleted: {
    5. log("log started");
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    and as expected, have access to this "Logger" components "log" method e.g.

    Qt Code:
    1. MouseArea {
    2. anchors.fill: parent;
    3. onClicked: {
    4. write.log("Mouse area clicked)
    5. console.log(convert.time_ms_to_sec(-1));
    6. console.log(convert.time_ms_to_sec(15000));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    This all works fine. My problem is that i now want to create some global functions inside my import library that can be called from anywhere without having to instantiate an object in every QML file. For example, i made a test function that converts milliseconds to seconds, but in order to use it i have to declare my "Conversion" object in each QML file, like..

    Qt Code:
    1. Conversion {
    2. id: convert;
    3. }
    4.  
    5. MouseArea {
    6. anchors.fill: parent;
    7. onClicked: {
    8. console.log(convert.time_ms_to_sec(15000));
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Is there a way of doing this without having to declare the "Conversion {}"?!? I want to be able to simply call convert.time_ms_to_sec(<val>) in any script that has my import.

    Thanks in advanced,

    Raab.
    Last edited by teh_raab; 15th May 2015 at 16:33.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt import library - creating a QML function

    You can make your plugin provide attached properties to objects or you can make it expose a global object using QQmlExtensionPlugin::initializeEngine() (or QDeclarativeExtensionPlugin::initializeEngine() if using Qt4). The latter would work similar to Math or Array objects in JavaScript.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2015
    Location
    West Sussex, England
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt import library - creating a QML function

    Thanks QDeclarativeExtensionPlugin sounds exactly like what i'm after. I'll give it a bash.

  4. #4
    Join Date
    Apr 2015
    Location
    West Sussex, England
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt import library - creating a QML function

    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
    Qt Code:
    1. plugin testplugin lib
    To copy to clipboard, switch view to plain text mode 

    testplugin.pro
    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += qt plugin
    3. QT += declarative
    4.  
    5. DESTDIR = lib
    6. OBJECTS_DIR = tmp
    7. MOC_DIR = tmp
    8.  
    9. HEADERS += conversions.h \
    10. testplugin.h
    11.  
    12. SOURCES += conversions.cpp \
    13. testplugin.cpp
    To copy to clipboard, switch view to plain text mode 

    conversions.h
    Qt Code:
    1. #ifndef CONVERSIONS_H
    2. #define CONVERSIONS_H
    3.  
    4. #include <QDeclarativeItem>
    5.  
    6. class Conversions : public QDeclarativeItem
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Conversions(QDeclarativeItem *parent = 0);
    12.  
    13. Q_INVOKABLE int time_ms_to_sec(int val);
    14. };
    15. #endif
    To copy to clipboard, switch view to plain text mode 

    testplugin.h
    Qt Code:
    1. #ifndef TESTPLUGIN_H
    2. #define TESTPLUGIN_H
    3.  
    4. #include <QDeclarativeExtensionPlugin>
    5.  
    6. class TestPlugin : public QDeclarativeExtensionPlugin
    7. {
    8. Q_OBJECT
    9. public:
    10. void registerTypes(const char *uri);
    11. };
    12. #endif
    To copy to clipboard, switch view to plain text mode 

    conversions.cpp
    Qt Code:
    1. #include "conversions.h"
    2.  
    3. Conversions::Conversions(QDeclarativeItem *parent)
    4. : QDeclarativeItem(parent)
    5. {
    6. // need to disable this flag to draw inside a QDeclarativeItem
    7. setFlag(QGraphicsItem::ItemHasNoContents, false);
    8. }
    9.  
    10.  
    11. int Conversions::time_ms_to_sec(int val = -1) {
    12. if(val == -1) {
    13. return -1;
    14. }
    15. else {
    16. return (val / 1000);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    testplugin.cpp
    Qt Code:
    1. #include "testplugin.h"
    2. #include "conversions.h"
    3. #include <qdeclarative.h>
    4.  
    5. void TestPlugin::registerTypes(const char *uri)
    6. {
    7. qmlRegisterType<Conversions>(uri, 1, 0, "Conversions");
    8. }
    9.  
    10. 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
    Qt Code:
    1. import QtQuick 1.0
    2. import TestPlugin 1.0
    3.  
    4. Item {
    5. width: 100; height: 100
    6.  
    7. Conversions {
    8. id: convv
    9. Component.onCompleted: {
    10. console.log("Test 1: " + convv.time_ms_to_sec(1000)); //Works ok
    11. console.log("Test 2: " + time_ms_to_sec(2000)); //Works ok
    12. }
    13. }
    14.  
    15. MouseArea {
    16. anchors.fill: parent;
    17. onClicked: {
    18. console.log("Test 3: " + convv.time_ms_to_sec(3000)); //Works ok
    19. console.log("Test 4: " + Conversions.time_ms_to_sec(4000)); //DOES NOT WORK
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 


    My final goal being..

    Main.qml
    Qt Code:
    1. import QtQuick 1.0
    2. import TestPlugin 1.0
    3.  
    4. Item {
    5. width: 100; height: 100
    6.  
    7. MouseArea {
    8. anchors.fill: parent;
    9. onClicked: {
    10. console.log("Test 4: " + Conversions.time_ms_to_sec(4000)); //DOES NOT WORK
    11. }
    12. }
    13. }
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt import library - creating a QML function

    Hmm... I even told you what function you were to reimplement

    Qt Code:
    1. class Conversions : public QObject {
    2. Q_OBJECT
    3. public:
    4. Conversions(QObject *parent = 0) : QObject(parent) {}
    5. public slots:
    6. int time_ms_to_sec(int val = -1) {
    7. return val == -1 ? -1 : (val / 1000);
    8. }
    9. };
    10.  
    11. void TestPlugin::initializeEngine(QDeclarativeEngine *engine, const char *url) {
    12. engine->rootContext()->setContextProperty("Conversions", new Conversions(engine));
    13. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2015
    Location
    West Sussex, England
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt import library - creating a QML function

    You Sir, are a legend It was pretty much what i had but could not work out the issue..

    I had to change

    Qt Code:
    1. engine->rootContext()->setContextProperty("Conversions", new Conversions(engine));
    To copy to clipboard, switch view to plain text mode 

    to be..

    Qt Code:
    1. engine->rootContext()->setContextProperty("_conversions", new Conversions(engine));
    To copy to clipboard, switch view to plain text mode 

    I could then call from QML -> "_conversions.time_ms_to_sec(4000)". Im assuming thats because i already have "Conversions" as a registered type ("qmlRegisterType<Conversions>(uri, 1, 0, "Conversions");")!

    Thanks once again.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt import library - creating a QML function

    The question is why you registered "Conversions". What is the point of doing that? How does your "Conversions" type differ from "Conversion"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2015
    Location
    West Sussex, England
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt import library - creating a QML function

    It shouldnt. That was just the original implementation that i wanted kept in until i got the above working. It has since been removed.

Similar Threads

  1. Best way of creating a library in qt
    By ehnuh in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2012, 16:10
  2. Delphi dll function import under QT
    By Gep in forum General Programming
    Replies: 6
    Last Post: 19th April 2011, 12:27
  3. dll build not creating import lib...
    By rickbsgu in forum Qt Tools
    Replies: 1
    Last Post: 11th August 2010, 19:45
  4. Replies: 4
    Last Post: 14th October 2009, 04:28
  5. import library linking
    By bruce1007 in forum Qt Programming
    Replies: 6
    Last Post: 19th May 2006, 10:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.