January 30, 2011

Daniel Eder Daniel Eder
Lab Rat
64 posts

Passing paramaters from Qt to QML via signal

 

Hi there,

I’ve got a project were the UI is designed and written in QML and the backend logic is written in C++ with Qt. What we want to do is using the well known signal-slot concept to pass some data between our C++ backend and the QML UI. I read many tutorials and webpages on how to share data between QML and Qt but I did not find any solution to my problem:

I want to emit a signal in Qt. This signal has got a parameter and this parameter has to be passed to the QML Userinterface. I already figured out how to catch the signal in QML but was not able to receive any paramters passed.

What I did so far is this:

We defined an object to pass and receive signals/slots between QML and Qt

  1. class QMLInterface : public QObject
  2. {
  3.     Q_OBJECT
  4.    ...
  5.  
  6.     signals:
  7.         void fileSelected(QString);
  8.    ...
  9.  
  10.     public slots:
  11.         void selectFile();
  12.     ...
  13. };

The function void QMLInterface::selectFile() is called and the idea is, that it emits the signal fileSelected(filename):

  1. void QMLInterface::selectFile()
  2. {
  3.     qDebug() << "selecting package file";
  4.     QString filename;
  5.     filename = QFileDialog::getOpenFileName(view, tr("Open Package"),
  6.                                             QDir::homePath(), "*.hhp");
  7.     emit fileSelected(filename);
  8. }

The QML Userinterface is able to receive the signal put I’ve got no idea how to process the paramters:

  1. TextInput{
  2.     id: filepath;
  3.     color: 'black';
  4.     Connections{
  5.         target: QMLInterface;
  6.         onFileSelected: test = 'no idea how to process any paramters...'
  7.     }
  8. }

So maybe anybody got some idea how to receive paramters from Qt? (Is it even possible to this the way we thought about it?)

7 replies

January 30, 2011

ngocketit ngocketit
Lab Rat
24 posts

When declaring the signal, you have to specify parameter names, e.g, instead of:

  1. signals:
  2.         void fileSelected(QString);

It should be:

  1. signals:
  2.         void fileSelected(QString fileName);

And then in QML, you access the passed parameter with this name, e.g:

  1. Connections{
  2.         target: QMLInterface;
  3.         onFileSelected: print('Selected file name: ' + fileName)
  4.     }

Hope it helps.

January 30, 2011

Daniel Eder Daniel Eder
Lab Rat
64 posts

Thank you! This works perfectly!

I’d never thought of specifing the variable in the signal signature.

April 14, 2012

Giorgos Tsiapaliwkas Giorgos Tsiapaliwkas
Lab Rat
67 posts

Hello,

sorry for bringing this thread again to life but I didn’t want to make a duplicate thread.

I understand the above, but if I want to call a method of QString fileName inside qml,
what should I do?

  1. onFileSelected: {
  2. print(fileName.someMethod()) //this one fails,its undefined
  3. }

 Signature 

terietor.gr

April 16, 2012

Uthering Uthering
Lab Rat
4 posts

I suppose, you need to declare it Q_INVOKABLE, look there: http://qt-project.org/doc/qt-4.8/qtbinding.html#calling-functions

April 16, 2012

Andre Andre
Area 51 Engineer
6031 posts

I don’t understand the question. fileName is of type QString (or rather, the QML equivalent of it), so it obviously doesn’t have the .someMethod() method defined. What do you want to achieve?

By the way, perhaps you should considder making fileName a property of your QMLInterface class (and of course emitting the signal as the NOTIFY signal of that property). That would allow you to simply bind to the property in QML, instead of using an explicit method. Much more in line with declarative programming.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 17, 2012

kahon kahon
Lab Rat
18 posts

to call a method of qobject from qml you have to define this like a public slot:
then it is callable.

April 18, 2012

chriadam chriadam
Ant Farmer
181 posts

Either a slot, or mark it as Q_INVOKABLE.

 
  ‹‹ How to minize/hide app with QML?      Can we bind a value back? ››

You must log in to post a reply. Not a member yet? Register here!