QT signal from C++ to QML

Asked

Viewed 424 times

0

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

The application opens using a QML and initiating a call to a SLOT in the responsible class. This class performs the reading and later will do some more work and return for a SIGNAL the work completed to be presented.

The main code 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 the work of the 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 teste is just a test I’m running to validate if I can receive the information later would return a QImage.

The SLOT who makes the call from SIGNAL to simplify the test.

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

Finally the QML, I will post only the party responsible for receiving the SIGNAL for the part of SLOT I have no problem at all.

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 documentation of Qt-project.org, but onTeste is not called at least the text in the place is not changed. In this case I used the qmlRegisterType because I couldn’t figure out a way to work the same way used to make SLOTS calls in C++ with engine.rootContext()->setContextProperty("filtro", &filtro);.

If anyone can help in receiving these signals. Grateful!

2 answers

2


Semi solved, for some reason Qt 5 is not accepting the Handler in the form.

Na main:

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

In qml:

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

To resolve, I did the following on main:

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

In the Imagefilter class I added a Q_PROPERTY I’ll put the entire code that makes it easier to view:

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 what I used:

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

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

1

I believe it is 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 may have more than one line using keys { ... }.

  • Yes, but as far as the documentation stated using {...}only for more than one line even, as what I want is only the alteration of a text there is no need to use it {...}.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.