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.