[Qt-qml] QML signal not firing c++ slot

Diego Marcos diego.marcos at gmail.com
Mon Aug 2 15:10:48 CEST 2010


Hi Chandraprakash,

This is how I'm calling now C++ slots from QML:

You define the slots in your C++ class:

	class MyClass : public QObject{

	Q_OBJECT

	public slots: 
       	void sayText(QString txt);
	
	}

You make an object of your class available within the declarative context:
	
        qmlContext->setContextProperty("slotsClass", new MyClass());

Now, you can call the slots from the QML:
	
	Component.onCompleted: {  slotsClass.sayText("Hello") }

I reported a bug related to this use case but it has not been evaluated yet. I define a slot in my class with a parameter and a default value for it. Whenever I call the slot, the default value overwrites the passed parameter by the caller. Please find a link with the report and a simple example:

http://bugreports.qt.nokia.com/browse/QTBUG-12293

I hope this is useful for you,

Diego.


On Aug 2, 2010, at 2:43 PM, Chandraprakash Sharma wrote:

> my qml file 
> ----------------------------------------------------------------
> import Qt 4.7
> import "qml"
> import "jscript.js" as CalcEngine
>  Item {
>      id:container
>      width: 1024
>      height: 768
> 
> 
>      signal ttsrequired(string txt);
>      function doOp(operation){CalcEngine.doOperation(operation)}
> 
>      Component.onCompleted: { pageLoader.source= "qml/Poke.qml"; container.ttsrequired("Hello"); console.log("event sent")}
>      Image {
>          source:"pics/robot_IR.jpg"
>          id: background
>          fillMode: Image.TileHorizontally
>          width: Math.max(pageLoader.width, parent.width)
>      }
> 
>      Loader { id: pageLoader
>          anchors.horizontalCenter:parent.horizontalCenter
>          anchors.verticalCenter:parent.verticalCenter
>          anchors.verticalCenterOffset: children[0].voff
> 
>      }
> 
>      Loader { id: pageLoaderin
>          source: pageLoader.children[0].src
>         anchors.horizontalCenter:parent.horizontalCenter
>         y:350
>     }
> 
>      Slider{
>          anchors.bottom: parent.bottom
>      }
> 
>      Button {x:930; y:0; width: 100; height: 100; keyc: "quit";}
> 
>      //onTtsrequired: console.log(txt + " QML");
>  }
> 
> slot in c++ code
> --------------------------------------------------------------
> public slots: void sayText(QString txt){
>         qDebug("Hello Debug");
>         QMessageBox msgBox;
>         msgBox.setText(txt);
>         msgBox.exec();
> }
> 
> Main.cpp file
> 
> #include <QApplication>
> #include <QGraphicsObject>
> #include <QDeclarativeView>
> #include <QDeclarativeContext>
> #include <TTSAdaptor.h>
> 
> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
> 
>     TTSAdaptor ttsAdaptor;
>     QDeclarativeView view;
>     view.setSource(QUrl::fromLocalFile("/home/saurabh/qtcreator-2.0.80/bin/TestNeel/start.qml"));
>     view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
> 
>     //view.showFullScreen();
> 
>     QDeclarativeContext *context = view.rootContext();
>     context->setContextProperty("backgroundColor",
>                                 QColor("#ffffff"));
> 
>     QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());
>     QObject::connect(rootObject, SIGNAL(ttsrequired(QString)), &ttsAdaptor, SLOT(sayText(QString)));
>     QObject::connect((QObject*)view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
> 
>     view.show();
>     return app.exec();
> }
> 
> signal ttsrequired is being fired in qml locally but c++ slot is not responding. I have tried many ways but not of help. Please revert if some problem is detected!
> 
> Regards,
> Chandraprakash
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-qml

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100802/e442b007/attachment.html 


More information about the Qt-qml mailing list