Connect a complex signal from QML to Qt: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]] | |||
'''English''' | [[Connect_a_complex_signal_from_QML_to_Qt_German|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 /><code><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 /></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("first&quot;, "second&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 <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 />