Results 1 to 6 of 6

Thread: importing C++ objects in QML, import PackageName VersionNumber not working

  1. #1
    Join Date
    Oct 2009
    Posts
    47
    Thanks
    10

    Question importing C++ objects in QML, import PackageName VersionNumber not working

    Hi,


    What I am trying to do here is create a communication from the QML front end to the C++ backend.

    I have a class in C++ named Dialpad with corresponding .h and .c files you can see here:

    Qt Code:
    1. #ifndef DIALPAD_H
    2. #define DIALPAD_H
    3.  
    4. #include <QObject>
    5.  
    6. class Dialpad : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit Dialpad(QObject *parent = 0);
    11.  
    12.  
    13. signals:
    14. void dialSignal(const QString &inNumberToDial);
    15. public slots:
    16. void dialSlot(const QString &inNumberToDial);
    17.  
    18.  
    19. };
    20.  
    21. #endif // DIALPAD_H
    22.  
    23. #include "dialpad.h"
    24. #include <QDebug>
    25.  
    26. Dialpad::Dialpad(QObject *parent) : QObject(parent)
    27. {
    28.  
    29.  
    30. }
    31.  
    32.  
    33.  
    34. /**
    35.   Name: dialSlot().
    36.   Description: this function is called from QML front end and sends the number to be dialed.
    37.   It will then emit the number to any functions who may need to number.
    38.   @param inNumberToDial this is the number that will be dialed
    39.   @return void
    40.   */
    41. void Dialpad::dialSlot(const QString &inNumberToDial)
    42. {
    43.  
    44. qDebug() << "Number recieved from QML frontend: " << inNumberToDial << endl;
    45. emit dialSignal(inNumberToDial);
    46.  
    47. }
    To copy to clipboard, switch view to plain text mode 

    I have a main.cpp that is doing the following:
    Qt Code:
    1. #include <QtGui/QApplication>
    2.  
    3. #include <QDeclarativeView>
    4. #include <QDeclarativeContext>
    5. #include <QDebug>
    6. #include <QList>
    7.  
    8. #include <QDeclarativeEngine>
    9. #include <QDeclarativeComponent>
    10. #include <QGraphicsDropShadowEffect>
    11.  
    12. #include "callinfo/callsonholdmodel.h"
    13. #include "dialpad/dialpad.h"
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QApplication a(argc, argv);
    18.  
    19.  
    20. qmlRegisterType<Dialpad>("DialpadPkg", 1, 0, "aDialpad");
    21.  
    22.  
    23. QDeclarativeView view;
    24. QDeclarativeContext *ctxt = view.rootContext();
    25. //ignore the setContext, this was for something else
    26. ctxt->setContextProperty("callsonholdModel", QVariant::fromValue(callsonholdList));
    27.  
    28. view.setSource(QUrl("qrc:qml/background.qml"));
    29. view.show();
    30.  
    31.  
    32. return a.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 


    Now when I try to import the object DialpadPkg so I can use aDialpad item to send a message from QML to C++ with the aDialpad.dialSlot("12345"); it says aDialpad is not a type, and it also says it can't import DialpadPkg 1.0


    here is the background.qml

    Qt Code:
    1. import Qt 4.7
    2. import "Dialpad"
    3. import "callinfo"
    4. import "buttontray"
    5. import "calendar"
    6. import DialpadPkg 1.0 //doesn't work
    7.  
    8.  
    9.  
    10.  
    11. Rectangle {
    12.  
    13.  
    14. //doesn't work
    15. aDialpad
    16. {
    17. id: interfaceDialpad
    18.  
    19. //interfaceDialpad.dialSlot("12345")
    20.  
    21. }
    22. ...
    23. ...
    To copy to clipboard, switch view to plain text mode 


    The directory stucture is the following:
    MainProject/main.cpp
    background.qml

    MainProject/Dialpad/dialpad.cpp
    dialpad.h


    Any ideas?

    Thanks

  2. #2
    Join Date
    May 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: importing C++ objects in QML, import PackageName VersionNumber not working

    never mind this post - something weird happened when editing.
    Last edited by bitflyer; 10th November 2010 at 10:49.

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

    Default Re: importing C++ objects in QML, import PackageName VersionNumber not working

    qmlRegisterType only registers the type in qml so that you can operate on its instances. It doesn't create a module, so you can't say "import DialpadPkg" in your code.
    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.


  4. #4
    Join Date
    Oct 2009
    Posts
    47
    Thanks
    10

    Default Re: importing C++ objects in QML, import PackageName VersionNumber not working

    How would I create an instance of it in QML so i can send info from QML to C++ what would I need to do if its not import? I already have a model that is sending information to QML to updating the QML but I need QML now to send info to C++. Such as pressing a button on QML I need it to send a string down to C++ end so I can do further processing.

    Thanks
    Last edited by technoViking; 10th November 2010 at 16:19.

  5. #5
    Join Date
    Oct 2009
    Posts
    47
    Thanks
    10

    Default Re: importing C++ objects in QML, import PackageName VersionNumber not working

    I got it to work but not sure if its the right way of doing it:
    Qt Code:
    1. QDeclarativeView view;
    2. QDeclarativeContext *ctxt = view.rootContext();
    3. ctxt->setContextProperty("myDialpad", new Dialpad());
    To copy to clipboard, switch view to plain text mode 
    I have a C++ class called Dialpad and you can access the object by refering to myDialpad in the QML code. So to send say a string to the C++ backend you would write the following in QML

    myDialpad.dialSlot(dialArea.text)

  6. #6
    Join Date
    May 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: importing C++ objects in QML, import PackageName VersionNumber not working

    Well, that's the way I did it first, too. You can also declare your signal&slot as Q_PROPERTY() for your C++ QObject. If you define the READ and WRITE and NOTIFY methods for the Q_PROPERTY, you can then treat it as a "normal" object in QML. So write in C++ class declaration something like

    Qt Code:
    1. Q_PROPERTY(QString numberToDial READ getNumberToDial WRITE dialSlot NOTIFY dialSlignal)
    To copy to clipboard, switch view to plain text mode 

    then you can write in QML:

    Qt Code:
    1. myDialpad.numberToDial = "12345"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Importing a QT .pro
    By salmanmanekia in forum Newbie
    Replies: 1
    Last Post: 16th April 2010, 19:17
  2. Replies: 3
    Last Post: 9th January 2010, 16:47
  3. importing headers
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 23:35
  4. importing from CAD model
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2007, 20:26
  5. Replies: 7
    Last Post: 18th July 2006, 22:33

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.