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

Signal from C++ in QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.7k 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.
  • T Offline
    T Offline
    Trikrista
    wrote on last edited by
    #1

    There is a code like this

    @
    // interfaceqml.h

    class InterfaceQML : public QObject {
    Q_OBJECT

    public:
    InterfaceQML(QObject *parent = 0);
    int t;

    signals:
    void newSignal(int i);

    public slots:
    void newSlot();
    };
    @

    @
    //interfaceqml.cpp

    InterfaceQML::InterfaceQML(QObject *parent) : QObject(parent) {
    t = 0;
    }

    void InterfaceQML::newSlot() {

    emit newSignal(t);
    t++;
    

    }
    @

    @
    //mainwindow.h

    class MainWindow : public QObject {
    Q_OBJECT
    public:
    MainWindow(QObject *parent = 0);
    InterfaceQML *interface;
    QQuickView *view;
    QTimer *timer;
    };
    @

    @
    //mainwindow.cpp

    MainWindow::MainWindow(QObject *parent) : QObject(parent) {
    interface = new InterfaceQML;
    timer = new QTimer;
    view = new QQuickView;

    qmlRegisterType<InterfaceQML>("Interface", 1, 0, "Interface");
    
    view->setSource(QUrl("file:/main.qml"));
    view->show();
    QObject *pObject = view->rootObject();
    
    connect(timer, SIGNAL(timeout()), interface, SLOT(newSlot()));
    timer->start(500);
    

    }
    @

    @
    //main.gml

    import QtQuick 2.0
    import Interface 1.0

    Rectangle {
    width: 500
    height: 500
    color: "#748596"

    signal sgSignal()
    
    Text {
        id: text
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
    
        text: "World"
        font.bold: true
        font.pointSize: 30
        color: "red"
    }
    
    Interface {
        onNewSignal: {
            text.text = i
        }
    }
    

    }
    @

    But signal not reach QML. How fix it?

    I'm sorry for my bad English

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are instantiation 2 separate Interface objects: one in C++, and another in QML. No wonder it does not work. Get a pointer to your QML instance using QObject::findChildren() (just remember that findChildren requires your QML object to be named in "objectName" property for the method to work) or use a timer in QML and trigger it from there.

      There are a few other ways to do it, but I won't flood you with information right now.

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Trikrista
        wrote on last edited by
        #3

        So I did.

        @
        //interfaceqml.cpp

        InterfaceQML::InterfaceQML(QObject *parent) : QObject(parent) {
            t = 0;
            emit newSignal(t);
        }
         ...
        

        @

        @
        //mainwindow.cpp

        MainWindow::MainWindow(QObject *parent) : QObject(parent) {
        timer = new QTimer;
        view = new QQuickView;

        qmlRegisterType<InterfaceQML>("Interface", 1, 0, "Interface");
        
        view->setSource(QUrl("file:/main.qml"));
        view->show();
        QObject *pObject = view->rootObject();
        InterfaceQML *interface = pObject->findChild<InterfaceQML *>();
        
        connect(timer, SIGNAL(timeout()), interface, SLOT(newSlot()));
        timer->start(500);
        

        }
        @

        Thanks

        I'm sorry for my bad English

        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