How to know if qmlRegisterType worked?



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



  • Did you import your module in your QML file?



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



  • 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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.