I'm not good at explaining so I'll just post some code. Marked the lines you should pay attention to with commends. Hope it helps 
mainwindow.h
#include <QMainWindow>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDebug>
Q_OBJECT
public:
view = new QDeclarativeView(this);
view->rootContext()->setContextProperty("mainwindow",this); //this
view
->setSource
(QUrl("mainwindow.qml"));
this->setCentralWidget(view);
}
private:
QDeclarativeView *view;
public slots:
void click() { //this
qDebug() << "click";
}
};
#include <QMainWindow>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDebug>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
view = new QDeclarativeView(this);
view->rootContext()->setContextProperty("mainwindow",this); //this
view->setSource(QUrl("mainwindow.qml"));
this->setCentralWidget(view);
}
private:
QDeclarativeView *view;
public slots:
void click() { //this
qDebug() << "click";
}
};
To copy to clipboard, switch view to plain text mode
mainwindow.qml
import QtQuick 1.0
Rectangle {
width: 100
height: 62
color: "red"
MouseArea {
anchors.fill: parent
onClicked: mainwindow.click() //this
}
}
import QtQuick 1.0
Rectangle {
width: 100
height: 62
color: "red"
MouseArea {
anchors.fill: parent
onClicked: mainwindow.click() //this
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks