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)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:snippets]]
{{LangSwitch}}
 
[[Category:Snippets::QML]]
'''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.
The example code below shows how you can connect complex signals from QML to Qt.


main.cpp
main.cpp
<code>
<code>
#include <QtGui>
#include <QtGui>
Line 14: Line 11:
class DeclarativeView : public QDeclarativeView
class DeclarativeView : public QDeclarativeView
{
{
Q_OBJECT
    Q_OBJECT
public:
public:
DeclarativeView(const QUrl &amp; source) : QDeclarativeView(source)
    DeclarativeView(const QUrl& source)  
{}
        : QDeclarativeView(source)
public slots:
    {
void testSlot(QString string1, QString string2)
    }
{
public slots:
qDebug() << string1 << string2;
    void testSlot(QString string1, QString string2)  
}
    {
        qDebug() << string1 << string2;
    }
};
};


#include "main.moc"
#include "main.moc"
int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
QString file = "main.qml";
    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);
DeclarativeView view(QUrl::fromLocalFile(file));


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


view.resize(200,200);
view.show();
return app.exec();
}
</code>


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


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


MouseArea {
    MouseArea {
anchors.fill: parent
        anchors.fill: parent
onClicked: viewClicked("first", "second")
        onClicked: viewClicked("first", "second")
}
    }
}
}
</code>
</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")
    }
}