I am working on something where I have a class called "FloatingMenu" (that is supossed to manage a menu in C++) and its QML alter-ego for the GUI in a file FloatingMenu.qml.
I have a file main.qml where I have a few rectangles and a FloatingMenu, and it shows up as expected.
Now, if I do qmlRegisterType("EasyGraph", 1,0, "FloatingMenu"); in my main.cpp, the graphical part of the FloatingMenu juts desappears.
Here is my code:
int main(int argc, char* argv[]){
QGuiApplication app(argc, argv);
//Register all the needed types in QML.
qmlRegisterType<FloatingMenu>("EasyGraph", 1,0, "FloatingMenu");
//The only window known as QDeCView in QML.
QQuickView* view = new QQuickView();
view->setMinimumHeight(100);
view->setMinimumWidth(100);
view->setResizeMode(QQuickView::SizeRootObjectToView);
//Context, to set properties from c++ to QML
//QQmlContext* cxt = view->rootContext();
//Load the base.
view->setSource(QUrl::fromLocalFile("../qml/MainWindow.qml"));
view->show();
//FloatingMenu* menu = view->rootObject()->findChild<FloatingMenu*>("mainMenu"); //When registered, this works
return app.exec();
}
and:
#ifndef _FLOATINGMENU_H_
#define _FLOATINGMENU_H_
#include <QQuickItem>
#include "GraphicalNode.hpp"
class FloatingMenu : public QQuickItem {
Q_OBJECT
Q_PROPERTY(GraphicalNode* target READ target WRITE setTarget NOTIFY onTargetNodeChanged)
private:
GraphicalNode* _target;
public:
FloatingMenu(QQuickItem* p = NULL);
GraphicalNode* target() const;
void setTarget(GraphicalNode* n);
signals:
void onTargetNodeChanged(GraphicalNode*);
};
#endif
#include "FloatingMenu.hpp"
FloatingMenu::FloatingMenu(QQuickItem* p) : QQuickItem(p), _target(NULL){
setFlag(QQuickItem::ItemHasContents, false); //I tried true and false here. what should it be? It has content, but I don't want to draw it from the C++ side.
}
GraphicalNode* FloatingMenu::target() const {
return _target;
}
void FloatingMenu::setTarget(GraphicalNode* n) {
_target = n;
}
my MainWindow.qml:
import EasyGraph 1.0
import QtQuick 2.0
Rectangle {
width: 400; height: 400
color: "#333333"
Flickable {
id: flickable
anchors.fill: parent
width: 400; height: 400
FloatingMenu{
objectName: "mainMenu"
id: menu
}
}
}
and finally, FloatingMenu.qml:
import EasyGraph 1.0
import QtQuick 2.0
Rectangle {
width: 30; height: 30
color: "red"
}
I have no idea what I am doing wrong, I dont see how its not working.
The only thing I want to do is to bind a QML object with a class and let the QML be the drawing part... I guess I am missing a little option or something that would make it work.
Thanks in advance!