Results 1 to 18 of 18

Thread: CPP QML communication

  1. #1
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default CPP QML communication

    Now I'm working with QT Quick Project -> QT Quick application. I loaded a QML file using QDeclaratieveView and did the CPP to QML and QML to CPP communication. It is worked...

    Can i do the same in any other QT applications like QT Widget-> QT GUI or Other Project??

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    For other GUI - yes. But, anyway, you need QDeclaratieveView instance.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    thanks for your replay.
    But I opened a "QT Widget->QT GUI App" and try to include QDeclarativeView.
    then i got the error QDeclarativeView: No such file or directory..

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    That's because you didn't add QT += declarative in your pro-file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    yes, it is worked...
    thank you very much.....

    I used
    Qt Code:
    1. qmlRegisterType<Widget>("File", 1,1, "TextUpdate");
    To copy to clipboard, switch view to plain text mode 
    to register the class in main and in the QML file I imported the file and called the function
    Qt Code:
    1. TextUpdate{
    2. id: sample1
    3. }
    To copy to clipboard, switch view to plain text mode 
    . then i got the visual c++ runtime library error..

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    What's an error message?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    That error message says that "The application has requested the runtime to terminate it in an unusual way"

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    We can only guess what's wrong without seeing your code.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    ok. I will show you my code.

    This is my Widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class Widget;
    8. }
    9.  
    10. class Widget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Widget(QWidget *parent = 0);
    16. ~Widget();
    17. void ShowQML();
    18. void setText(QString);
    19.  
    20. private:
    21. Ui::Widget *ui;
    22. };
    23.  
    24. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    This is my Widget.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include <QtGui>
    4. #include <QDeclarativeComponent>
    5. #include <QDeclarativeEngine>
    6. #include <QDeclarativeItem>
    7. #include <QDeclarativeView>
    8.  
    9. Widget::Widget(QWidget *parent) :
    10. QWidget(parent),
    11. ui(new Ui::Widget)
    12. {
    13.  
    14. ui->setupUi(this);
    15. qDebug()<<"Inside cnstrctr";
    16. }
    17. void Widget::ShowQML()
    18. {
    19. QDeclarativeView *testView = new QDeclarativeView();
    20. testView->setSource(QUrl::fromLocalFile("test2.qml"));
    21. testView->show();
    22. }
    23.  
    24. Widget::~Widget()
    25. {
    26. delete ui;
    27. }
    28. void Widget::setText(QString msg)
    29. {
    30. qDebug()<<"Inside slot in CPP";
    31. //SendSignal();
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    this is the main file
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "widget.h"
    3. #include <qdeclarative.h>
    4. #include <QDeclarativeView>
    5. #include <QApplication>
    6. #include <QBoxLayout>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. qmlRegisterType<Widget>("File", 1,1, "TextUpdate");
    11. QApplication a(argc, argv);
    12. Widget w;
    13. //w.show();
    14. w.ShowQML();
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    and the QML file is
    Qt Code:
    1. // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    2. import QtQuick 1.1
    3. import File 1.1
    4.  
    5. Rectangle {
    6. width: 100
    7. height: 62
    8. TextUpdate{
    9. id: sample1
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    First of all you have memory leak in Widget::ShowQML.
    Second, are you trying to register Widget (which inherits QWidget) here qmlRegisterType<Widget>("File", 1,1, "TextUpdate");?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    Yes, Widget is my class name. I'm trying to register with that class name.

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    That's not possible. You can only register classes inherited from QObject. I know that QWidget inherits QObject, but you can't register widgets for using them in QML.


    Added after 4 minutes:


    QML relies heavily on Qt's meta object system and can only instantiate classes that derive from QObject. For visual element types, this will usually mean a subclass of QDeclarativeItem; for models used with the view elements, a subclass of QAbstractItemModel; and for arbitrary objects with properties, a direct subclass of QObject.
    See Extending QML Functionalities using C++.
    Last edited by spirit; 21st August 2012 at 10:25.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #13
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    yes, now it is worked...
    one more doubt
    In my project i have lots of CPP to QML communication. Then which UI is best for me, QT Quick or QT Widget?

  14. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    It depends on what kind of UI do you what: modern fancy live ui (with animations and other stuff) or old school ui.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  15. The following user says thank you to spirit for this useful post:

    nestuser (21st August 2012)

  16. #15
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    I'm not developing QML file. I need only communication part.

  17. #16
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    So, you are working on the back-end. Then you should concentrate on architecting of the back-end structure. If you make the back-end independent from ui then you (or your colleagues) will be able to choose the best ui approach: QML or Qt widget-based.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. The following user says thank you to spirit for this useful post:

    nestuser (21st August 2012)

  19. #17
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: CPP QML communication

    thank you very much..
    I'm working on back-end. I need to emit only some signals to QML file.
    I think QT widget is apt for it, right??

  20. #18
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPP QML communication

    Yes, it is.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  21. The following user says thank you to spirit for this useful post:

    nestuser (27th August 2012)

Similar Threads

  1. Communication with USB
    By prasenjit in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2010, 00:19
  2. Qt and .Net communication
    By soniaerm in forum Qt Programming
    Replies: 0
    Last Post: 22nd April 2010, 06:37
  3. Example of SSL communication
    By Morea in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2009, 23:41
  4. USB communication
    By M. Bashir in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2008, 02:56
  5. Communication Help Pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 25th May 2006, 13:22

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.