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. How to expose model to QML
Qt 6.11 is out! See what's new in the release blog

How to expose model to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 723 Views 2 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.
  • R Offline
    R Offline
    Raghvendra
    wrote on last edited by A Former User
    #1

    Hey There, I need to expose model in qml file,

    like

    mail.cpp

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

    QStandardItemModel model(4, 4);
    for (int row = 0; row < 4; ++row) {
        for (int column = 0; column < 4; ++column) {
            QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
            model.setItem(row, column, item);
        }
    }
    MyClass myc(model);
    
    //qDebug()<<"Data from model "<<myc.
    //MyClass myc;
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    engine.rootContext()->setContextProperty("myc", &myc);
    engine.rootContext()->setContextProperty("datamodel", &model);
    
    
    return app.exec();
    

    -----------------------------main.qml is

    Rectangle {

            id: graphContainer
            visible: false
            x: 0
            y: 200
            width: 630
            height: 443
    
           gradient: Gradient {
                GradientStop {
                    position: 0
                    color: "#ffffff"
                }
    
                GradientStop {
                    position: 1
                    color: "#000000"
                }
            }
    
           Graph
           {
               id:graphChart
               model:datamodel
    
           }
    
        }
    

    but it gives me error

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi

      • "but it gives me error"
        Always include the actual error with as much info as possible. :)
      1 Reply Last reply
      2
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        said in How to expose model to qml:

        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        engine.rootContext()->setContextProperty("myc", &myc);
        engine.rootContext()->setContextProperty("datamodel", &model);

        Perhaps you should try setting model before setting the QML.

        157

        1 Reply Last reply
        2

        • Login

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