Connect a complex signal from QML to Qt

From Qt Wiki
Revision as of 09:20, 24 February 2015 by Maintenance script (talk | contribs)
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 &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;

class DeclarativeView : public QDeclarativeView<br />{<br /> Q_OBJECT<br />public:<br /> DeclarativeView(const QUrl &amp; source) : QDeclarativeView(source)<br /> {}<br /> public slots:<br /> void testSlot(QString string1, QString string2)<br /> {<br /> qDebug() &lt;&lt; string1 &lt;&lt; string2;<br /> }<br />};

#include &quot;main.moc&amp;quot;<br />int main(int argc, char **argv)<br />{<br /> QString file = &quot;main.qml&amp;quot;;

QApplication app(argc, argv);<br /> DeclarativeView view(QUrl::fromLocalFile&amp;amp;#40;file&amp;amp;#41;);

QDeclarativeItem '''item = qobject_cast&amp;lt;QDeclarativeItem'''&gt;(view.rootObject());<br /> QObject::connect(item, SIGNAL (viewClicked(QString , QString)), &amp;view, SLOT (testSlot(QString, QString)));

view.resize(200,200);<br /> view.show();<br /> return app.exec&amp;amp;#40;&amp;#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(&quot;first&amp;quot;, &quot;second&amp;quot;)<br /> }<br />}<br />