Results 1 to 2 of 2

Thread: invoking qml from cpp

  1. #1
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt4

    Question invoking qml from cpp

    Hi everyone,

    i've been reading the forums for some time but now i need some help

    I tried to boil it down to the most simples case to show it here:

    I have a qml file simply taken from one of the official tutorials.
    It's a simple button. lets call it button2.

    I have a cpp code, that generates a window and a button, button1.
    Once the button1 is pressed, QDeclarativeView is used to view the qml file and a window pops up showing button1.

    Pressing button2 (the qml button) should connect to a cpp slot that will show a message on terminal.

    Everything works fine, but when i click on button2 i get a segfault.

    Here is the code.
    I was not sure if i'm supposed to post the entire code (with all the indcludes and stuff).
    I hope its ok to post the code in such a way.


    Qt Code:
    1. //Button.qml
    2. import QtQuick 1.0
    3.  
    4. Rectangle
    5. {
    6. id: button
    7. width: 75; height: 50
    8.  
    9. Text
    10. {
    11. id: buttonLabel
    12. anchors.centerIn: parent
    13. text: "click here"
    14. }
    15.  
    16. MouseArea
    17. {
    18. id: buttonMouseArea
    19. anchors.fill: parent
    20. onClicked: buttonClick()
    21. }
    22.  
    23. signal buttonClick()
    24. onButtonClick:
    25. {
    26. qmlSlot.cppMethod("Hello from QML") //this is the cpp slot i whant to call
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //myqml.h
    2.  
    3. class MyClass : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MyClass(QWidget *parent = 0);
    9.  
    10. private slots:
    11. void showQmlSlot(); //this is the slot to view the qml file
    12. Q_INVOKABLE void cppMethod(const QString &msg) {
    13. qDebug() << "Called the C++ method with" << msg;
    14. }//this is the cpp slot to be called within the qml file
    15.  
    16. private:
    17. QPushButton *click;
    18. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //myqml.cpp
    2.  
    3. MyClass::MyClass(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. click = new QPushButton("Click me", this);
    7. click->setGeometry(50, 40, 75, 30);
    8.  
    9. connect(click, SIGNAL(clicked()),
    10. this, SLOT(showQmlSlot()));
    11. }
    12.  
    13.  
    14. void MyClass::showQmlSlot()
    15. {
    16. MyClass myClass;
    17.  
    18. QDeclarativeView *view = new QDeclarativeView();
    19.  
    20. view->setSource(QUrl::fromLocalFile("Button.qml"));
    21. view->rootContext()->setContextProperty("qmlSlot", &myClass);
    22.  
    23. view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    24. view->show();
    25. }
    To copy to clipboard, switch view to plain text mode 


    I just dont get where the error comes from.
    Funny enought, if i use the QDeclarativeView within the main.cpp,
    like shown for instance here
    http://www.developer.nokia.com/Commu...ation_from_QML
    it works fine...

    Thanks for helping me

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: invoking qml from cpp

    Disclaimer: I am a total QML newb. That said: your invokable function is a private slot. Can you call a private cpp function from qml? Somehow, I doubt it...

Similar Threads

  1. Invoking another application
    By Qting in forum Qt Programming
    Replies: 5
    Last Post: 6th February 2012, 16:35
  2. qmake not invoking uic?
    By Regenlied in forum Newbie
    Replies: 7
    Last Post: 9th August 2011, 16:07
  3. invoking Matlb through QT
    By sal in forum Newbie
    Replies: 2
    Last Post: 22nd May 2010, 22:43
  4. Invoking New Dialog
    By kb1lqc in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 15:23
  5. Invoking a browser...???
    By deepusrp in forum Newbie
    Replies: 4
    Last Post: 12th June 2007, 17:32

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.