<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to expose dynamically amount of data to QML.]]></title><description><![CDATA[<p dir="auto">I have an app were I need to fetch  random questions from a database and expose them to qml dynamically.</p>
<p dir="auto">So I created a class to store each dataset:</p>
<pre><code>class Question : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int id READ id)
    Q_PROPERTY(QString askedQuestion READ askedQuestion)
public:
    Question(int id,
             QString askedQuestion);

    int getId() const;
    QString getAskedQuestion() const;

private:
    int mId;
    QString mAskedQuestion;
};
</code></pre>
<p dir="auto">And fill them in annother class. In reality it is derrived from an SQLDatabaseModel:</p>
<pre><code>class QuestionGenerator : public QObject
{
    Q_OBJECT
public:
    explicit QuestionGenerator(QObject *parent = nullptr);

    Q_INVOKABLE QVector&lt;Question&gt; getRandomQuestions(int count) const
    {
        // simplified in reality we fetch random questions from a database. 
        // the point is we need to add Questions to the vector
        // but this does not work since QObject based items cannot get copied
        QVector&lt;Question&gt; questions;
        questions.reserve(count);
        
        // add questions to vector
        
        return questions;
    }
};
</code></pre>
<p dir="auto">I want to expose <code>Question</code> to QML to use the data from <code>Question</code> there so I need to derive it from <code>QObject</code>.</p>
<p dir="auto">When I fetch the Questions randomly in <code>QuestionGenerator</code> it does not work because <code>QVector</code> does net the not supported copy constructor of <code>QObject</code>.</p>
<p dir="auto">So how can I fix this?</p>
<p dir="auto">Again what I want:</p>
<p dir="auto">Fetch n Questions in C++ and expose them to QML so I can use the data to display.</p>
]]></description><link>https://forum.qt.io/topic/119338/how-to-expose-dynamically-amount-of-data-to-qml</link><generator>RSS for Node</generator><lastBuildDate>Sun, 10 May 2026 18:07:25 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/119338.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Sep 2020 14:17:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to expose dynamically amount of data to QML. on Fri, 25 Sep 2020 16:13:33 GMT]]></title><description><![CDATA[<p dir="auto">I went for exposing a QQmlListProperty&lt;Question&gt; to QML. It works fine.</p>
]]></description><link>https://forum.qt.io/post/618926</link><guid isPermaLink="true">https://forum.qt.io/post/618926</guid><dc:creator><![CDATA[sandro4912]]></dc:creator><pubDate>Fri, 25 Sep 2020 16:13:33 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose dynamically amount of data to QML. on Fri, 25 Sep 2020 15:52:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sandro4912">@<bdi>sandro4912</bdi></a><br />
several options described here<br />
<a href="https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html</a></p>
]]></description><link>https://forum.qt.io/post/618924</link><guid isPermaLink="true">https://forum.qt.io/post/618924</guid><dc:creator><![CDATA[ODБOï]]></dc:creator><pubDate>Fri, 25 Sep 2020 15:52:21 GMT</pubDate></item></channel></rss>