Important: Please read the Qt Code of Conduct -

Registering a type makes the QML code not work

  • Hello,

    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<FloatingMenu>("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();

    //Context, to set properties from c++ to QML
    //QQmlContext* cxt = view->rootContext();

    //Load the base.

    //FloatingMenu* menu = view->rootObject()->findChild<FloatingMenu*>("mainMenu"); //When registered, this works

    return app.exec();

    @#ifndef FLOATINGMENU_H
    #define FLOATINGMENU_H

    #include <QQuickItem>
    #include "GraphicalNode.hpp"

    class FloatingMenu : public QQuickItem {
    Q_PROPERTY(GraphicalNode* target READ target WRITE setTarget NOTIFY onTargetNodeChanged)
    GraphicalNode* _target;
    FloatingMenu(QQuickItem* p = NULL);
    GraphicalNode* target() const;
    void setTarget(GraphicalNode* n);

    void onTargetNodeChanged(GraphicalNode*);


    #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

    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 think 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...

  • Can you show the application output pls?!
    Most often you can find the mistake there! ;)


  • Hi, thanks for your answer.
    There is no output (no error messages or anything).
    I forgo to post my main.qml code. I just updated my previous post! :)

  • Oh, I think I found the mistake:
    You have a FloatingMenu.qml and then you call:
    @qmlRegisterType<FloatingMenu>("EasyGraph", 1,0, "FloatingMenu");
    Don't you have two types with the same name then?! ;)


  • Thanks Dot97.
    I tried what you said, but it didn't work... (I remaned everything in QML to FloatingMenuQML).. I think it is ok to have that with two names....
    So I still have the problem...

    Thanks again anyway!

Log in to reply