[solved][Qt5] How to correctly registerType?



  • I have problem with Qt5 that I don't know what happen. I tried to get data from C++ object but it shows some weird behaviour to me.

    This is my main.cpp
    @#include <QCoreApplication>
    #include <QObject>
    #include <QtDeclarative/QDeclarativeItem>
    #include <QQmlContext>

    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"

    #include "myapi.h"
    #include "itemlistmodel.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    // ItemListModel *itemListModel = new ItemListModel;

    MyAPI myAPI;
    

    // myAPI.setItemListModel(itemListModel);

    QtQuick2ApplicationViewer viewer;
    
    qRegisterMetaType<ItemListModel*>("ItemListModel");
    qmlRegisterType<ItemListModel>("Models",1,0,"ItemListModel");
    
    QQmlContext *context = viewer.rootContext();
    context->setContextProperty("MyAPI", &myAPI);
    

    // context->setContextProperty("ItemListModel", itemListModel);

    viewer.setMainQmlFile&#40;QStringLiteral("qml/untitled/main.qml"&#41;&#41;;
    viewer.showExpanded();
    
    return app.exec&#40;&#41;;
    

    }@

    This is main.qml.
    @import QtQuick 2.0

    Rectangle {
    id: appWindow

    width: 360
    height: 360
    
    Text {
        id: textElement
        anchors { top: parent.top; left: parent.left; right: parent.right }
        height: 50
        text: "page " + myApi.itemListModel.currentPage
    }
    
    ListView {
        id: itemListViewElement
        anchors { fill: parent; topMargin: textElement.height}
        model: myApi.itemListModel
    
        Component.onCompleted: console.log(myApi.itemListModel + " - " + myApi.itemListModel.currentPage);
    }
    

    }@
    the output is
    @QVariant(ItemListModel*) - undefined@

    but data shown correctly in itemListViewElement

    but if I uncomment commented lines in main.cpp and modify main.qml to
    @import QtQuick 2.0

    Rectangle {
    id: appWindow

    width: 360
    height: 360
    
    Text {
        id: textElement
        anchors { top: parent.top; left: parent.left; right: parent.right }
        height: 50
        text: "page " + ItemListModel.currentPage
    }
    
    ListView {
        id: itemListViewElement
        anchors { fill: parent; topMargin: textElement.height}
        model: ItemListModel
    
        Component.onCompleted: console.log(ItemListModel + " - " + ItemListModel.currentPage);
    }
    

    }@

    the output is correct
    @ItemListModel(0x8c0e50) - 1@

    and data still shown correctly in itemListViewElement

    in "myapi.h" I've already declare
    @ Q_PROPERTY(ItemListModel* itemListModel
    READ getItemListModel
    CONSTANT
    )@

    same with "itemlistmodel.h"
    @ Q_PROPERTY(int currentPage
    READ getCurrentPage
    WRITE setCurrentPage
    NOTIFY currentPageChanged
    )@

    So how do I resolve this problem so that I can use something like myApi.itemListModel.currentPage to be worked in qml file and without add my variable to setContextPropery?
    I found it's weird because I've tried something similar with Qt4 and it worked without a problem.



  • My bad.
    I know where is the problem now.

    I shouldn't use
    @#include <QtDeclarative/QDeclarativeItem>@

    the problem solved after change it to
    @#include <QtQml>@


Log in to reply
 

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