0

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

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. =)

ViGi
  • 140
  • 1
  • 11

1 Answers1

1

You can use QQmlContext::setContextProperty to set a value for your name property on the root context :

engine.rootContext()->setContextProperty("w1",  &w1);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Wow all errors disappeared except for one: no matching function for call to 'Wuerfel::roll(QString&)' case 0: roll(*reinterpret_cast< QString*>(_v)); break; ^ – ViGi Aug 24 '14 at 17:21
  • 2
    @ViGi Your property declaration says `WRITE roll`, which means your `roll()` method should be a setter. – MrEricSir Aug 24 '14 at 17:40
  • @MrEricSir So I have to change the roll() to roll(QString bla) ? Did that and now I got something that looks like a constructor problem to me: undefined reference to `Wuerfel::Wuerfel(QObject*)' geez.. Thanks for the help guys. I see progress but goal still isn't reached :( – ViGi Aug 24 '14 at 17:58
  • Okay I removed Constructor, now the windows at least pops up ^^ I guess I will try to figure stuff out alone. The value does not change etc and it says that w1 is not defined, but uff.. Thanks guys for your trouble =) – ViGi Aug 24 '14 at 18:05
  • 1
    @ViGi `roll` should have an `int` argument. It should be like `void roll(int value)`. Also you should not emit `rolled()` there because it would be emitted automatically as it is defined as `NOTIFY`. – Nejat Aug 24 '14 at 18:18
  • @All Jay! Got i working. Only part was missing was [link](http://qtvon0auf100.wordpress.com/2013/11/19/c-signal-nach-qml-slot/) Sorry it's german, I mean the connections part in qml, after that I everything went fine. P.S. when I remove the emit rolled(), it does nothing, like the signal is not emitted, maybe it's not automatic. But again thanks for all your help, finally a positive result =) – ViGi Aug 24 '14 at 18:29