Created
October 28, 2013 09:11
-
-
Save Kakadu/7193656 to your computer and use it in GitHub Desktop.
QtQuick demo about signal in QML and slot in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui/QGuiApplication> | |
#include "signalslotlistview.h" | |
#include <QtQuick/QQuickItem> | |
#include <QtQuick/QQuickView> | |
int main(int argc, char *argv[]) | |
{ | |
QGuiApplication app(argc, argv); | |
QQuickView *view = new QQuickView(); | |
view->setSource(QUrl::fromLocalFile("qml/testsignal/main.qml")); | |
QObject *rect = dynamic_cast<QObject*>(view->rootObject()); | |
SignalslotlistView myClass; | |
QObject::connect(rect, SIGNAL(qmlSignal(QString)), | |
&myClass, SLOT(cppSlot(QString))); | |
view->show(); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.0 | |
Rectangle { | |
id: test | |
width: 200 | |
height: 50 | |
x: 10 | |
y: 10 | |
signal qmlSignal(string msg) | |
MouseArea { | |
hoverEnabled: false | |
anchors.fill: parent | |
onClicked: { | |
console.log("geklickt") | |
test.qmlSignal("asdf") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SIGNALSLOTLISTVIEW_H | |
#define SIGNALSLOTLISTVIEW_H | |
#include <QObject> | |
#include <QDebug> | |
class SignalslotlistView : public QObject | |
{ | |
Q_OBJECT | |
public: | |
explicit SignalslotlistView(QObject *parent = 0); | |
signals: | |
public slots: | |
void cppSlot(const QString &msg) { | |
qDebug() << "received " << msg; | |
} | |
}; | |
#endif // SIGNALSLOTLISTVIEW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "signalslotlistview.h" | |
SignalslotlistView::SignalslotlistView(QObject *parent) : | |
QObject(parent) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOURCES += main.cpp \ | |
signalslotlistview.cpp | |
HEADERS += \ | |
signalslotlistview.h | |
QT += quick | |
OTHER_FILES += \ | |
qml/testsignal/main.qml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment