Connect a complex signal from QML to Qt: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Connecting a complex signal from <span class="caps">QML</span> to Qt=
[[Category:snippets]]


The example code below shows how you can connect complex signals from <span class="caps">QML</span> to Qt.
'''English''' | [[Connect_a_complex_signal_from_QML_to_Qt_German|Deutsch]]


main.cpp<br />
= Connecting a complex signal from QML to Qt =


main.qml<br />
The example code below shows how you can connect complex signals from QML to Qt.


===Categories:===
main.cpp<br /><code><br />#include &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;


* [[:Category:snippets|snippets]]
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 /></code>
 
main.qml<br /><code><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 /></code>

Revision as of 09:20, 24 February 2015


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 />