QT signal from C ++ to QML

0

I'm trying with QT 5 + QML to create an application where after opening the file using fileDialog it returns a message by a QT signal.

The application opens using a QML and initiates a call to a SLOT in the class responsible. This class reads and then does some more work and returns a% w / o of the finished work to be submitted.

The code for main follows below:

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "imagefilter.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

 ImageFilter filtro;
 qmlRegisterType<ImageFilter>("ImageFilter", 1, 0, "ImageFilter");

 QQmlApplicationEngine engine;
 engine.rootContext()->setContextProperty("filtro", &filtro);
 engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

 return app.exec();
}

The class responsible for working with images:

#include <QObject>
#include <QString>
#include <QDebug>

class ImageFilter : public QObject
{
 Q_OBJECT
public:
 explicit ImageFilter(QObject *parent = 0);

signals:
 void teste(const QString &path);
public slots:
 void receiveImage(const QString path);

};
  

The SIGNAL is just a test that I am performing to validate if I can receive the information later it would return a signal teste .

The QImage that calls SLOT to simplify the test.

void ImageFilter::receiveImage(QString path)
{
 qDebug() << path;
 emit teste("Teste");
}

Finally QML, I will post only the part responsible for receiving SIGNAL because the part of SIGNAL I do not have any problem.

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Dialogs 1.2
import ImageFilter 1.0

ImageFilter {
  onTeste: image2.text: path
}

What would be expected to work because I used the qt-project.org documentation, but the onTest is not called at least the in-place text is not changed. In this case I used SLOT because I did not find a way to work the same as that used to make SLOTS calls in C ++ with qmlRegisterType .

If anyone can help in how to receive these signals. Thanks!

    
asked by anonymous 16.07.2014 / 23:50

2 answers

2

Semi-resolved, for some reason Qt 5 is not accepting the handler of the form.

Na main:

qmlRegisterType<ImageFilter>("org.image.filter", 1, 0, "ImageFilter");

No qml:

ImageFilter {
    id: document
    onUrlChanged: {
        console.log("fui alterado")
    }
}

To solve, I did the following in main:

ImageFilter filter
engine.rootContext()->setContextProperty("filtro", &filter);

In the ImageFilter class I added a Q_PROPERTY I will put the whole code that is easier to visualize:

class Filter : public QObject
{
   Q_OBJECT
   Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)

public:
   explicit Filter(QObject *parent = 0);

   void setUrl(const QUrl url){
      if (m_url != url) {
         m_url = url;
         emit urlChanged();
      }
   }

   QUrl url() const {
      return m_url;
   }

signals:
   void urlChanged();

public slots:
   void imageSlot(const QUrl &url){
      qDebug() << url;
      setUrl(url);
   }

private:
   QUrl m_url;

};

And in the qml I used:

Connections {
    target: filtro
    onUrlChanged: {
        console.log("url alterada")
        image2.text = filtro.url
    }
}

With this I managed to make the handler work, some things are different, in this case my signal is urlChanged ();

    
21.07.2014 / 22:09
1

I think it's merely a syntax error with your QML file:

ImageFilter {
  onTeste: image2.text = path
}

The value after onTeste must be a valid javascript code, not a definition of another property. You can have more than one line using { ... } keys.

    
17.07.2014 / 15:11