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 7: Line 7:
The example code below shows how you can connect complex signals 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 &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;
main.cpp
<code>
#include <QtGui>
#include <QtDeclarative>


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 />};
class DeclarativeView : public QDeclarativeView
{
Q_OBJECT
public:
DeclarativeView(const QUrl &amp; source) : QDeclarativeView(source)
{}
public slots:
void testSlot(QString string1, QString string2)
{
qDebug() << string1 << string2;
}
};


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


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


QDeclarativeItem '''item = qobject_cast&amp;lt;QDeclarativeItem'''&gt;(view.rootObject());<br /> QObject::connect(item, SIGNAL (viewClicked(QString , QString)), &amp;view, SLOT (testSlot(QString, QString)));
QDeclarativeItem '''item = qobject_cast<QDeclarativeItem'''>(view.rootObject());
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>
view.resize(200,200);
view.show();
return app.exec();
}
</code>


main.qml<br /><code><br />import QtQuick 1.0
main.qml
<code>
import QtQuick 1.0


Item {<br /> width: 200; height: 300<br /> signal viewClicked(string first, string second)
Item {
width: 200; height: 300
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>
MouseArea {
anchors.fill: parent
onClicked: viewClicked("first", "second")
}
}
</code>

Revision as of 09:42, 25 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

#include <QtGui>
#include <QtDeclarative>

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

#include "main.moc"
int main(int argc, char **argv)
{
 QString file = "main.qml";

QApplication app(argc, argv);
 DeclarativeView view(QUrl::fromLocalFile(file));

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

view.resize(200,200);
 view.show();
 return app.exec();
}

main.qml

import QtQuick 1.0

Item {
 width: 200; height: 300
 signal viewClicked(string first, string second)

MouseArea {
 anchors.fill: parent
 onClicked: viewClicked("first", "second")
 }
}