Results 1 to 2 of 2

Thread: How to expose dynamically amount of data to QML

  1. #1
    Join Date
    Sep 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to expose dynamically amount of data to QML

    I have an app were I need to fetch random questions from a database and expose them to qml dynamically.

    So I created a class to store each dataset:


    Qt Code:
    1. class Question : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(int id READ id)
    5. Q_PROPERTY(QString askedQuestion READ askedQuestion)
    6. public:
    7. Question(int id,
    8. QString askedQuestion);
    9.  
    10. int getId() const;
    11. QString getAskedQuestion() const;
    12.  
    13. private:
    14. int mId;
    15. QString mAskedQuestion;
    16. };
    To copy to clipboard, switch view to plain text mode 


    And fill them in annother class. In reality it is derrived from an SQLDatabaseModel:



    Qt Code:
    1. class QuestionGenerator : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit QuestionGenerator(QObject *parent = nullptr);
    6.  
    7. Q_INVOKABLE QVector<Question> getRandomQuestions(int count) const
    8. {
    9. // simplified in reality we fetch random questions from a database.
    10. // the point is we need to add Questions to the vector
    11. // but this does not work since QObject based items cannot get copied
    12. QVector<Question> questions;
    13. questions.reserve(count);
    14.  
    15. // add questions to vector
    16.  
    17. return questions;
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 


    I want to expose `Question` to QML to use the data from `Question` there so I need to derive it from `QObject`.

    When I fetch the Questions randomly in `QuestionGenerator` it does not work because `QVector` does net the not supported copy constructor of `QObject`.

    So how can I fix this?


    Again what I want:

    Fetch n Questions in C++ and expose them to QML so I can use the data to display.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to expose dynamically amount of data to QML

    When I fetch the Questions randomly in `QuestionGenerator` it does not work because `QVector` does net the not supported copy constructor of `QObject`.

    So how can I fix this?
    Store Question * instead of Question in your QVector. Use qDeleteAll() followed by QVector::clear() to delete the contents of the QVector without a memory leak.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 20th November 2016, 18:10
  2. Dynamically change data to QCharts
    By Blitzor DDD in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2016, 17:45
  3. Replies: 34
    Last Post: 22nd March 2016, 18:34
  4. Storing and displaying "large" amount of data
    By FreddyKay in forum Qt Programming
    Replies: 4
    Last Post: 27th November 2014, 20:31
  5. Replies: 0
    Last Post: 26th July 2012, 10:32

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.