Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Signal not emitted from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

Signal not emitted from C++ to QML

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FlyingSheep
    wrote on last edited by
    #1

    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.75
    1000)
    {
    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&#40;&#41;;
    

    };@

    @//main.qml
    import QtQuick 1.1
    import com.nokia.meego 1.0

    PageStackWindow {
    id: appWindow
    initialPage: mainPage
    MainPage {
    id: mainPage
    }
    };@

    @//MainPage.qml
    import QtQuick 1.1
    import com.nokia.meego 1.0
    import Torch 1.0

    Page {
    id: page

    Torch {
        id: torch
        onFlashTimeChanged: console.log("FlashTimeChanged: " + flashTime)
    }
    
    Button {
        y: 100
        text: "double flashTime"
        onClicked: torch.flashTime = torch.flashTime * 2;
    }
    

    };@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexleutgoeb
      wrote on last edited by
      #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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved