I went on #qt-quick on free node, and w00t helped me.
Here's the fixed code:
test.h:
#ifndef TEST_H
#define TEST_H
#include <QObject>
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
explicit Test(QObject *parent = 0);
QString text() const;
void setText(QString txt);
signals:
void textChanged();
public slots:
void testSlot();
protected:
QString m_text = "Default String";
};
#endif // TEST_H
test.cpp:
#include "test.h"
#include <QDebug>
using namespace std;
Test::Test(QObject *parent) : QObject(parent)
{
}
void Test::testSlot() {
qDebug() << "Test::testSlot called" << endl;
this->setText("test slot called");
}
QString Test::text() const {
return this->m_text;
}
void Test::setText(QString txt) {
this->m_text = txt;
emit textChanged();
}
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <test.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
Test t;
engine.rootContext()->setContextProperty("Test", &t);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *item = engine.rootObjects().at(0);
QObject::connect(item, SIGNAL(buttonPressed()),
&t, SLOT(testSlot()));
return app.exec();
}
main.qml:
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
ApplicationWindow {
id: mainWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
signal buttonPressed();
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("&Open")
onTriggered: console.log("Open action triggered");
}
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
MainForm {
anchors.fill: parent
button1.onClicked: block.text = "prev clicked"
button2.onClicked: mainWindow.buttonPressed()
block.text: Test.text
}
MessageDialog {
id: messageDialog
title: qsTr("May I have your attention, please?")
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}
Thanks everyone!