I'm starting out with QT5.3, or rather QT in general. Now I basically want to program C/C++ console applications and add a front-end.
I created a QT Quick Application and have trouble getting my back-end code to interact with the front-end.
What I have so far:
Main.qml :
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
Window {
visible: true
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
// Qt.quit();
}
}
Text {
text: w1.getRoll
anchors.centerIn: parent
}
Button {
onClicked: w1.roll
}
}
Main.cpp :
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "wuerfel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
Wuerfel w1;
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
engine.setContextForObject(&w1,engine.rootContext());
return app.exec();
}
Wuerfel.h :
#ifndef WUERFEL_H
#define WUERFEL_H
#include <QObject>
#include <time.h>
#include <cstdlib>
class Wuerfel : public QObject
{
Q_OBJECT
Q_PROPERTY(QString w1 READ getRoll WRITE roll NOTIFY rolled)
public:
explicit Wuerfel(QObject *parent = 0);
void roll(){
srand((unsigned) time(NULL));
head = rand() % 6 + 1;
emit rolled();
}
int getRoll(){
return head;
}
signals:
void rolled();
public slots:
private:
int head;
};
#endif // WUERFEL_H
Debug Error
I have no clue what I have to do. The Documentation and web search results with similar issues confuse me even more. They mention QQView
or QComponent
etc. but whenever I try one of their solutions, something is missing. Like the method mentioned is not part of the object, so it's not found etc.
Has anyone a clue how to get this working? I want to use this approach to visualize future console applications from a C++ tutorial. And developing front-ends in QT in general.
Thanks in Advance. =)
You can use QQmlContext::setContextProperty
to set a value for your name property on the root context :
engine.rootContext()->setContextProperty("w1", &w1);