Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Registering a type makes the QML code not work
Forum Updated to NodeBB v4.3 + New Features

Registering a type makes the QML code not work

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    excalibur1491
    wrote on last edited by
    #1

    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();
    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 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...

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Alexander Linne
      wrote on last edited by
      #2

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

      Regards

      1 Reply Last reply
      0
      • E Offline
        E Offline
        excalibur1491
        wrote on last edited by
        #3

        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! :)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Alexander Linne
          wrote on last edited by
          #4

          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?! ;)

          Regards

          1 Reply Last reply
          0
          • E Offline
            E Offline
            excalibur1491
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved