Connect a complex signal from QML to Qt
Jump to navigation
Jump to search
English | Deutsch
Connecting a complex signal from QML to Qt
The example code below shows how you can connect complex signals from QML to Qt.
main.cpp
<br />#include <QtGui&gt;<br />#include <QtDeclarative&gt;
class DeclarativeView : public QDeclarativeView<br />{<br /> Q_OBJECT<br />public:<br /> DeclarativeView(const QUrl & source) : QDeclarativeView(source)<br /> {}<br /> public slots:<br /> void testSlot(QString string1, QString string2)<br /> {<br /> qDebug() << string1 << string2;<br /> }<br />};
#include "main.moc&quot;<br />int main(int argc, char **argv)<br />{<br /> QString file = "main.qml&quot;;
QApplication app(argc, argv);<br /> DeclarativeView view(QUrl::fromLocalFile&amp;#40;file&amp;#41;);
QDeclarativeItem '''item = qobject_cast&lt;QDeclarativeItem'''>(view.rootObject());<br /> QObject::connect(item, SIGNAL (viewClicked(QString , QString)), &view, SLOT (testSlot(QString, QString)));
view.resize(200,200);<br /> view.show();<br /> return app.exec&amp;#40;&#41;;<br />}<br />
main.qml
<br />import QtQuick 1.0
Item {<br /> width: 200; height: 300<br /> signal viewClicked(string first, string second)
MouseArea {<br /> anchors.fill: parent<br /> onClicked: viewClicked("first&quot;, "second&quot;)<br /> }<br />}<br />