PDA

View Full Version : QML to Qt and back



forty-two
1st July 2012, 16:45
Hi,

How can I call a Qt public slot in the onTextChanged event of a QML-Text, that edits the source attribute of an QML-Image?

What I basically would like to know is how to express the following in QML:


QObject::connect(QML-Text, SIGNAL(onTextChanged()), SomeClass, SLOT(editSource());


The second challenge then would be to change the source attribute of the QML-Image.
What I figured out by now is, that there's some QDeclarativeEngine class, however I don't really get how to implement it. Isn't there just some simple way to change attributes like it's possible with ui-files?


ui->myLabel->setPixmap(pixmap);


Thanks in advance,
forty-two

wysota
1st July 2012, 17:05
It depends whether you want to do that from C++ or QML. If the former, then you need to access a particular object using the declarative engine. If the latter then you can for example use the Connections QML statement.

forty-two
1st July 2012, 18:21
Thanks for the quick reply,

I would like to do the first task in QML and the second one in Qt/C++.

If there's a class "MyClass" derived from QObject with a public slot called "editSource(QString text)", how would I connect this public slot from the class in QML?


Text {
id: myText
onTextChanged: {
// call the Qt slot from MyClass here
}
}


and how would I use QDeclarativeEngine in the Qt/C++ slot to change the source?

How would that look in code?

wysota
1st July 2012, 21:59
You don't connect slots (or signals) to classes but rather to instances. So first you need an instance of the class and then you need to expose it to QML using QDeclarativeContext::setContextProperty(). Then it's just a matter of calling the instance's method in line #4 of your code snippet.