Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to know if qmlRegisterType worked?
Qt 6.12 LTS is out! See what's new in the release blog

How to know if qmlRegisterType worked?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.4k 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.
  • Q Offline
    Q Offline
    Qnoobish
    wrote on last edited by
    #1

    Hello,

    Currently I am having a bad time trying to register a C++ class as a new type to the QML system.

    This is my code:
    @
    qmlRegisterType<ItemsFilter>("backEndComponents", 1,0, "ItemsFilter");
    qmlRegisterType<SelectionRecovery>("backEndComponents", 1,0, "SelectionRecovery");

    QQuickView *poView = new QQuickView;
    poView->setSource(QUrl::fromLocalFile("pkg/visufrwk/pkg/plugins/adapt/dummy.qml"));

    QMainWindow *poMainWindow = new QMainWindow(this);
    QWidget *poWidget = new QWidget(poMainWindow);
    poWidget = QWidget::createWindowContainer(poView);
    poMainWindow->setCentralWidget(poWidget);
    
    poMainWindow->show();
    

    @

    Every time I try to load my QML I get the error that the module "backEndComponents" is not installed.

    So how could I know if qmlRegisterType was successful? All I know from that function is that it returns with a type ID.

    I wonder what could be wrong in my approach?

    In short, software is eating the world.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Did you import your module in your QML file?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qnoobish
        wrote on last edited by
        #3

        Yes. In the file dummy.qml, I imported the "backEndComponents" like this:

        @
        import QtQuick 2.1
        import backEndComponents 1.0

        Rectangle{
        color: "blue"
        width: 100
        height:100
        }
        @

        If I comment the line with the import to "backEndComponents" I could see my blue Rectangle, if not I get the "module not installed" error.

        So, I am not sure what I am missing.

        In short, software is eating the world.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nando76
          wrote on last edited by
          #4

          Hi,
          i use it like this:

          main.cpp:
          @
          qmlRegisterType<MyData> ("de.mycompany.qmlcomponents", 1, 0, "MyData");
          QtQuick2ApplicationViewer viewer;
          QQmlContext *ctxt = viewer.rootContext();

          viewer.setSource(QUrl("qrc:/resources/qml/main.qml"));
          viewer.showExpanded();
          @

          and then in qml file:
          @
          import de.mycompany.qmlcomponents 1.0
          @

          Hope it helps,
          Nando

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t3685
            wrote on last edited by
            #5

            That should do the trick normally. Probably something else going on. I don't think qmlRegisterType ever fails.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              Qnoobish
              wrote on last edited by
              #6

              Hello,

              I think I found the error. I tested my code with Qt 5.2.1 and it didn't work. So I decided to use another version of Qt that I have installed in my PC, Qt 5.1.0, and it worked.
              So now at least I know my code is working and the problem resides somewhere else among my installation. Maybe I have a corrupted library that is causing this error.

              In short, software is eating the world.

              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
              • Unsolved