Results 1 to 16 of 16

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

  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.

  2. #2
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    It is not about QQmlApplicationEngine. Nor anything between TableView and QAbstractListModel. I made a simple test with Animal example and it showed everything ok.

    So the problem must be in that I have many models. Or models are not ready somehow when qml loaded and the ListView gets updated afterwards and TableView isn't.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    You are not exporting rokmodel as an object but as a value. Are you sure you want to do that?

    Btw, since you also export "this" as "product", why do those hacky connects at the end? Would be better to just handle the signals in QML.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    kiwimess (12th September 2015)

  5. #4
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    I was just coming to tell that this was really a rookie mistake. Of course the data vanished in the end of constructor because of this: ComboModel combo; Changed it to pointer -> problem solved.

    anda_skoa, do you mean using js in QML to handle signals? I'm having this Q_PROPERTY(QString sum READ sum WRITE setSum NOTIFY sumChanged) in my class and I change the product.sum value in QML from the class. I try to use only C++ and no js in this project.

    Thanks for the help Anda.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    I meant something like this:
    Qt Code:
    1. onListSignal: product.listSignal(nameOfSignalArgument)
    To copy to clipboard, switch view to plain text mode 
    I.e. using the QML signal handler and calling the C++ object's slot.

    That way you don't need to make any assumptions in C++ on what the QML scene looks like (currently it assumes that the first object has those signals).

    Cheers,
    _

  7. #6
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    Ok, I am doing just that to find out which line clicked:
    Qt Code:
    1. onClicked: applicationWindow.listSignal(tableView.currentRow)
    To copy to clipboard, switch view to plain text mode 

    Product is different. It is meant to change the sum text in QML from C++.

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    I see.

    Then you could just do
    Qt Code:
    1. onClicked: product.listSignal(tableView.currentRow)
    To copy to clipboard, switch view to plain text mode 
    i.e. calling the slot "listSignal" on object "product" directly.

    Cheers,
    _

  9. #8
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    I have one more question to you, if you like to help a bit more...

    When I run this in my testprogram main, it shows line data ok.
    Qt Code:
    1. LineModel model;
    2.  
    3. model.addLine(Line("Candy", "Ben", "10"));
    4. model.addLine(Line("Paper", "Ben", "20"));
    5. model.addLine(Line("Matches", "Ben", "30"));
    6.  
    7. QQmlApplicationEngine *engine = new QQmlApplicationEngine;
    8. QQmlContext *ctxt = engine->rootContext();
    9. ctxt->setContextProperty("myModel", &model);
    10. engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
    11.  
    12. Line line = model.get(1);
    13. qDebug() << line.article();
    14. qDebug() << line.person();
    15. qDebug() << line.price();
    To copy to clipboard, switch view to plain text mode 

    But when I run it from in my another programs class where linemodel is (now) member value (I hope this is said right).
    Now the program crashes, when run this function.
    Qt Code:
    1. .h:
    2. LineModel *linemodel;
    3. Line *oneline;
    4.  
    5. class:
    6. //Line *oneline = new Line;
    7. Line oneline = linemodel->get(1);
    8. qDebug() << oneline.article();
    To copy to clipboard, switch view to plain text mode 

    And the get, which is the same in working and not working programs.

    Qt Code:
    1. Line LineModel::get(int index) const
    2. {
    3. const Line line = m_lines[index];
    4. return line;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kiwimess; 13th September 2015 at 15:06.

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    is the linemodel pointer valid?
    Is the index valid?

    Cheers,
    _

  11. #10
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    Qt Code:
    1. h:
    2. private: Linemodel *linemodel; Line *oneline;
    3.  
    4. constructor:
    5. LineModel *linemodel = new LineModel;
    6. linemodel->addLine(Line("Book", "1,40", "Heidi"));
    7. linemodel->addLine(Line("Paper", "2,40", "Heidi"));
    8. linemodel->addLine(Line("Pen", "12,40", "Heidi"));
    To copy to clipboard, switch view to plain text mode 

    And here in beginning of line 1. debugger says that "linemodel not accessible". Doesn't the *linemodel in constructor hold the data? And the private members are available to everywhere inside that class? This is inside a slot (shouldn't make any difference?)
    Qt Code:
    1. Line oneline = linemodel->get(0);
    2. qDebug() << oneline.article();
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    [QUOTE=kiwimess;281990]
    Qt Code:
    1. constructor:
    2. LineModel *linemodel = new LineModel;
    To copy to clipboard, switch view to plain text mode 
    If you really have that in your constructor then you have the "linemodel" member uninitialized.

    Quote Originally Posted by kiwimess View Post
    And the private members are available to everywhere inside that class?
    Yes.
    But if you code above is actually what you have, then you are not accessing the member but shadowing it with a local variable.

    Cheers,
    _

  13. #12
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    So now when I have LineModel *linemodel in header as private member, I need to make a function to pass it as reference to be used in main. And this way all the data is usable in this class later? Sorry for simple questions, I'm simple person

  14. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    As a pointer, since you need a pointer to export it as a context property.

    But you better post updated code, otherwise we might be talking about very different things.

    Cheers,
    _

  15. #14
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    You read my mind, I was thinking the same.
    Actually what I'm trying to do is add and remove lines from the list in c++. And hope that list updates itself automaticly in QML.

    Qt Code:
    1. class CashRegister : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QString sum READ sum WRITE setSum NOTIFY sumChanged)
    5. public:
    6. explicit CashRegister(QObject *parent = 0);
    7. QString sum();
    8. void setSum(QString sum);
    9. signals:
    10. void sumChanged();
    11. public slots:
    12. void buttonPressed(const QString &type, const QString &id);
    13. void listSignal(const QString &index);
    14. private:
    15. QQmlApplicationEngine *engine;
    16. QList<QObject*> dataList;
    17. QString m_sum;
    18. QString userName;
    19. QQmlContext *ctxt;
    20. LineModel *linemodel; // SHOULNT THIS SAVE THE DATA?
    21. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. CashRegister::CashRegister(QObject *parent) : QObject(parent)
    2. {
    3. QQmlApplicationEngine *engine = new QQmlApplicationEngine;
    4. ComboModel combo;
    5. lst << "Book" << "Pen" << "Eraser" << "Liner" << "Notebook";
    6. combo.setComboList(lst);
    7.  
    8. // TODO: dataList omaan luokkaa pois tästä luokasta
    9.  
    10. LineModel *linemodel = new LineModel;
    11. linemodel->addLine(Line("Book", "1,40", "Heidi"));
    12. linemodel->addLine(Line("Paper", "2,40", "Heidi"));
    13. linemodel->addLine(Line("Pen", "12,40", "Heidi"));
    14.  
    15. QQmlContext *ctxt = engine->rootContext();
    16. ctxt->setContextProperty("rokmodel", linemodel);
    17. ctxt->setContextProperty("myModel", QVariant::fromValue(combo.comboList()));
    18. ctxt->setContextProperty("comboModel", &combo);
    19. ctxt->setContextProperty("product", this);
    20.  
    21. engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
    22.  
    23. QObject::connect(engine->rootObjects().takeFirst(), SIGNAL(qmlSignal(QString, QString)), this, SLOT(buttonPressed(QString, QString)));
    24. QObject::connect(engine->rootObjects().takeFirst(), SIGNAL(listSignal(QString)), this, SLOT(listSignal(QString)));
    25.  
    26. Line oneline = linemodel->get(0);
    27. qDebug() << oneline.article();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Here last two lines work ok.

    But when used in code below -> explosion.

    Qt Code:
    1. void CashRegister::buttonPressed(const QString &type, const QString &id) {
    2. qDebug() << "Called the C++ slot with message:" << type << id;
    3. - - - -
    4. else if(type == "add") {
    5. qDebug() << "add pressed!";
    6. //Line *oneline = new Line;
    7. Line oneline = linemodel->get(0);
    8. qDebug() << oneline.article();
    9. return;
    10. }
    11. - - - -
    To copy to clipboard, switch view to plain text mode 

    So this last linemodel (7.) is not the same than in constructor (and I know answer is not very compilated but my matches are wet).

  16. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    In CashRegister::buttonPressed() you are accessing the member "linemodel", which is never initialized.
    An uninitialized pointer can point to anywhere, in most cases any attempt to access it will cause a crash.

    Your problem, as pointed out earlier, is in line 11 of your constructor.
    Instead of using a local variable, it should be initializing the member.

    Cheers,
    _

  17. The following user says thank you to anda_skoa for this useful post:

    kiwimess (14th September 2015)

  18. #16
    Join Date
    Sep 2015
    Posts
    9
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: QAbstractListModel data to QML TableView why not shown through QQmlApplicationEng

    Qt Code:
    1. linemodel = new LineModel;
    To copy to clipboard, switch view to plain text mode 
    It works! (I'm dancing here )
    Thank you very much Anda for your help!

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.