Skip to content

Instantly share code, notes, and snippets.

@Kakadu
Created October 28, 2013 09:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kakadu/7193656 to your computer and use it in GitHub Desktop.
Save Kakadu/7193656 to your computer and use it in GitHub Desktop.
QtQuick demo about signal in QML and slot in C++
#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();
}
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")
}
}
}
#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
#include "signalslotlistview.h"
SignalslotlistView::SignalslotlistView(QObject *parent) :
QObject(parent)
{
}
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