Signal not emitted from C++ to QML
-
Hi all
I have created a qml project using a C++ plugin (Torch). A simplified version of the code is pasted below.
Torch offers a Q_PROPERTY flashTime.
If I set this property from QML (e.g. via the button on MainPage), then I receive the onFlashTimeChanged signal in QML. (OK)
However if I change this property from within C++ by the setFlashTime function (e.g. called from the class constructor), then in QML I do not receive the onFlashTimeChanged signal. (NOT OK).
I have been running this on Harmattan on a Nokia N9.
Any ideas? I am sure I have missed something small ...
Chris
@//Torch.h
#ifndef TORCH_H
#define TORCH_H#include <QDeclarativeItem>
#include <QDebug>class Torch : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(int flashTime READ flashTime WRITE setFlashTime NOTIFY flashTimeChanged)
public:
explicit Torch(QDeclarativeItem *parent = 0);
~Torch();
int flashTime() const;
void setFlashTime(int flashTime);signals:
void flashTimeChanged(const int &flashTime);public slots:
protected slots:
private:
int _flashTime;
}
#endif // TORCH_H;@@//Torch.cpp
#include "Torch.h"
#include <QDebug>Torch::Torch(QDeclarativeItem parent) :
QDeclarativeItem(parent)
,_flashTime(0.751000)
{
setFlashTime(760);
}Torch::~Torch()
{
}int Torch::flashTime() const
{
return _flashTime;
}void Torch::setFlashTime(int flashTime)
{
qDebug() << "c++ setFlashTime called";
if (flashTime == _flashTime)
{
qDebug() << "value is same ...";
return;
}
qDebug() << "about to emit flashTimeChanged";
_flashTime = flashTime;
emit flashTimeChanged(flashTime);
};@@//main.cpp
#include <QApplication>
#include "qmlapplicationviewer.h"
#include "Torch.h"Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
qmlRegisterType<Torch>("Torch",1,0,"Torch");
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/flashTime/main.qml"));
viewer.showExpanded();return app->exec();
};@
@//main.qml
import QtQuick 1.1
import com.nokia.meego 1.0PageStackWindow {
id: appWindow
initialPage: mainPage
MainPage {
id: mainPage
}
};@@//MainPage.qml
import QtQuick 1.1
import com.nokia.meego 1.0
import Torch 1.0Page {
id: pageTorch { id: torch onFlashTimeChanged: console.log("FlashTimeChanged: " + flashTime) } Button { y: 100 text: "double flashTime" onClicked: torch.flashTime = torch.flashTime * 2; }
};@
-
Have you also tried calling the method outside from the constructor? It may be the case that the bindings are not already up and working at this place. As an alternative you can try to call setFlashTime from componentComplete (a virtual method you can override in your Torch class).
Alex