Results 1 to 16 of 16

Thread: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEngine

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default QAbstractListModel data to QML TableView why not shown through QQmlApplicationEngine

    I want my data to be showed in QML in table. When I follow this example here: http://doc.qt.io/qt-5/qtquick-models...l-example.html and add the TableView and use QQuickView in main all data is showed fine.
    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.2
    3.  
    4. Rectangle {
    5. height: 300
    6. width: 600
    7. //![0]
    8. ListView {
    9. width: 200; height: 250
    10.  
    11. model: myModel
    12. delegate: Text { text: "Animal: " + type + ", " + size }
    13. }
    14. //![0]
    15.  
    16. TableView {
    17. y:150
    18. width: 600
    19. model: myModel
    20. TableViewColumn {
    21. role: "type"
    22. title: "Type"
    23. width: 200
    24. }
    25. TableViewColumn {
    26. role: "size"
    27. title: "Size"
    28. width: 200
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QGuiApplication app(argc, argv);
    2.  
    3. AnimalModel model;
    4. model.addAnimal(Animal("Wolf", "Medium"));
    5. model.addAnimal(Animal("Polar bear", "Large"));
    6. model.addAnimal(Animal("Quoll", "Small"));
    7.  
    8. QQuickView view;
    9. view.setResizeMode(QQuickView::SizeRootObjectToView);
    10. QQmlContext *ctxt = view.rootContext();
    11. ctxt->setContextProperty("myModel", &model);
    12. //![0]
    13.  
    14. view.setSource(QUrl("qrc:view.qml"));
    15. view.show();
    16.  
    17. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    But when I made my own implementation and wanted to use QQmlApplicationEngine, I get all the data fine to ListView but TableView is empty. And I just don't get it. Is it because of QQmlApplicationEngine or what?

    Ok, this is not in main, it is in other classes constructor (doesn't make difference, does it?).

    Qt Code:
    1. QQmlApplicationEngine *engine = new QQmlApplicationEngine;
    2.  
    3. ComboModel combo;
    4. lst << "Geelikynnet" << "Ripset" << "Kampaus" << "Tatuointi" << "Kulmat";
    5. combo.setComboList(lst);
    6.  
    7. LineModel rokmodel;
    8. rokmodel.addLine(Line("Kulmat", "155,00", "Heidi"));
    9. rokmodel.addLine(Line("Tatuointi", "255,00", "Heidi"));
    10. rokmodel.addLine(Line("Kynnet", "55,00", "Ann-Marii"));
    11. rokmodel.addLine(Line("Ripset", "5,00", "Ann-Marii"));
    12.  
    13. QQmlContext *ctxt = engine->rootContext();
    14. ctxt->setContextProperty("rokmodel", QVariant::fromValue(&rokmodel));
    15. ctxt->setContextProperty("myModel", QVariant::fromValue(combo.comboList()));
    16. ctxt->setContextProperty("comboModel", &combo);
    17. ctxt->setContextProperty("product", this);
    18.  
    19. engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
    20. QObject::connect(engine->rootObjects().takeFirst(), SIGNAL(qmlSignal(QString, QString)), this, SLOT(buttonPressed(QString, QString)));
    21. QObject::connect(engine->rootObjects().takeFirst(), SIGNAL(listSignal(QString)), this, SLOT(listSignal(QString)));
    To copy to clipboard, switch view to plain text mode 

    I have very thin understanding about models, but I understood that QAbstractListModel is the best because the view is updated automatically if I add items to the list. ListView would be fine to me, but I dont get the text in columns.
    Last edited by kiwimess; 11th September 2015 at 22:36.

Similar Threads

  1. QQuickView or QQmlApplicationEngine or QQuickWidget
    By ustulation in forum Qt Quick
    Replies: 0
    Last Post: 18th January 2015, 13:16
  2. QQmlApplicationEngine crashing (QT5 and VS 2010)
    By leonardo.massei in forum Qt Programming
    Replies: 0
    Last Post: 28th January 2014, 13:09
  3. Replies: 1
    Last Post: 20th October 2011, 10:08
  4. Replies: 4
    Last Post: 5th March 2010, 14:20
  5. getting data from tableView
    By mkarakaplan in forum Newbie
    Replies: 1
    Last Post: 7th November 2007, 09:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.