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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Sub-categorize)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:snippets]]
{{LangSwitch}}
[[Category:Snippets::QML]]
The example code below shows how you can connect complex signals from QML to Qt.


'''English''' | [[Connect_a_complex_signal_from_QML_to_Qt_German|Deutsch]]
main.cpp


= Connecting a complex signal from QML to Qt =
<code>
#include <QtGui>
#include <QtDeclarative>


The example code below shows how you can connect complex signals from QML to Qt.
class DeclarativeView : public QDeclarativeView
{
    Q_OBJECT
public:
    DeclarativeView(const QUrl& source)
        : QDeclarativeView(source)
    {
    }
public slots:
    void testSlot(QString string1, QString string2)
    {
        qDebug() << string1 << string2;
    }
};


main.cpp<br /><code><br />#include &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;
#include "main.moc"


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;;
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)), &view, SLOT (testSlot(QString, QString)));
   
    view.resize(200,200);
    view.show();
    return app.exec();
}
</code>


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)));
main.qml


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

Latest revision as of 12:31, 28 November 2016

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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& 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)), &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")
    }
}