<?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[subclass queryModel]]></title><description><![CDATA[<p dir="auto">Hello,<br />
I want to create my own querymodel class to make some edits to my data model.<br />
However, I am new to this. I created my own model class as below and implemented some functions.<br />
my model class works fine on data with 0 to 200 rows.<br />
But it works a bit slow on data with 1000-2000 rows. How can I optimize this model class to run the fastest?<br />
I am sharing my code with you. Can you tell me my mistakes or where it could be better?</p>
<p dir="auto">For example, I am using a 2D string array as a datasource in the class. Is this method correct?<br />
<strong>myModel.h :</strong></p>
<pre><code>#define MYMODEL_H

#include &lt;QSqlQueryModel&gt;
#include &lt;QObject&gt;
#include &lt;QSqlRecord&gt;


class myModel: public QSqlQueryModel
{



    Q_OBJECT
public:
    explicit myModel(QString **dataList,QStringList headerDataList,QObject *parent = nullptr,int rowcount=0,int colcount=0);

public:
    QVariant data(const QModelIndex &amp;index, int role) const;
    int rowCount(const QModelIndex &amp;parent) const;
    int columnCount(const QModelIndex &amp;parent) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    Qt::ItemFlags flags(const QModelIndex &amp;index) const;

private:    
      QSqlQueryModel *_model;
      QString **_dataList;
      QStringList _headerDataList;
      const int _rowCount,_colCount;


};




#endif // MYMODEL_H

</code></pre>
<p dir="auto"><strong>myModel.cpp :</strong></p>
<pre><code>#include "myModel.h"

myModel::myModel(QString **dataList,QStringList headerDataList, QObject *parent,int rowcount,int colcount) :
    QSqlQueryModel{parent},_dataList(dataList),_headerDataList(headerDataList),_rowCount(rowcount),_colCount(colcount)
{




}

QVariant myModel::data(const QModelIndex &amp;index, int role) const
{
    if (!index.isValid())
           return QVariant();
    if(index.row()&gt;_rowCount-1 || index.column()&gt;_colCount-1)
        return QVariant();

    switch (role) {
    case Qt::DisplayRole:
        return  _dataList[index.row()][index.column()];
    case Qt::TextAlignmentRole:
        return Qt::AlignCenter;
    }
    return QVariant();
    //return _model-&gt;record(index.row()).value(index.column());
}



int myModel::rowCount(const QModelIndex &amp;parent) const
{
    //return _model-&gt;rowCount();
    return _rowCount;
}

int myModel::columnCount(const QModelIndex &amp;parent) const
{
    //return _model-&gt;columnCount();
    return _colCount;
}

QVariant myModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal)
    {
        if(role == Qt::DisplayRole)
        {
            return _headerDataList.at(section);
            //return _model-&gt;headerData(section,orientation);
        }
   }

    return QVariant();
}

Qt::ItemFlags myModel::flags(const QModelIndex &amp;index) const
{
    return  QSqlQueryModel::flags(index) |Qt::ItemIsDragEnabled;
}




</code></pre>
]]></description><link>https://forum.qt.io/topic/139487/subclass-querymodel</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 16:32:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139487.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 22 Sep 2022 06:49:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 14:31:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nevez">@<bdi>Nevez</bdi></a> said in <a href="/post/729814">subclass queryModel</a>:</p>
<blockquote>
<p dir="auto">So when I run the project, it loads 1000 rows and 10 columns of data to the _dataList in about 1-2 minutes. ( In the for loop I showed above)</p>
</blockquote>
<p dir="auto">It "cannot" take that long to execute those statements 1,000 times so I don't know what to say.  Can only suggest you step through in debugger or set up a completely standalone program to do what you show and try there.</p>
<blockquote>
<p dir="auto">sorry, i didn't understand completely. Can you show an example from my code?</p>
</blockquote>
<p dir="auto">For</p>
<pre><code>columnList.append(_model-&gt;data(_model-&gt;index(row,col)).toString());
</code></pre>
<p dir="auto">if you have access to the "datalist" for your dedicated <code>_model</code> you are reading from, something like:</p>
<pre><code>columnList.append(_the_model_datalist[col][row]);
</code></pre>
<p dir="auto">to avoid going through <code>_model-&gt;data()</code> etc.</p>
<p dir="auto">But now that you have said "minutes" to do the copying loop this should be irrelevant in comparison to that as-yet-unexplained slowness.</p>
]]></description><link>https://forum.qt.io/post/729815</link><guid isPermaLink="true">https://forum.qt.io/post/729815</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 22 Sep 2022 14:31:45 GMT</pubDate></item><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 14:08:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/729805">subclass queryModel</a>:</p>
<blockquote>
<p dir="auto">"Uzun bir süre" tanımlayın. "Çok az" zaman almalıdır.</p>
</blockquote>
<p dir="auto">Yes, I should have explained this part a little more. So when I run the project, it loads 1000 rows and 10 columns of data to the _dataList in about 1-2 minutes. ( In the for loop I showed above)<br />
Don't you think it's a long time?</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/729805">subclass queryModel</a>:</p>
<blockquote>
<p dir="auto">If you want fastest, why do you go through your existing model at the _model-&gt;data(_model-&gt;index(row,col)) level?</p>
</blockquote>
<p dir="auto">what should I do ?</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/729805">subclass queryModel</a>:</p>
<blockquote>
<p dir="auto">If you have access to the underlying actual model/data (like your _dataList) you can read from that directly to avoid the model indexing overhead.</p>
</blockquote>
<p dir="auto">sorry, i didn't understand completely. Can you show an example from my code?</p>
]]></description><link>https://forum.qt.io/post/729814</link><guid isPermaLink="true">https://forum.qt.io/post/729814</guid><dc:creator><![CDATA[Nevez]]></dc:creator><pubDate>Thu, 22 Sep 2022 14:08:13 GMT</pubDate></item><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 13:20:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nevez">@<bdi>Nevez</bdi></a> said in <a href="/post/729789">subclass queryModel</a>:</p>
<blockquote>
<p dir="auto">For example, a model with 1000 rows and 15 columns takes a long time to load into these vectors. Is there a more convenient or faster method for this?</p>
</blockquote>
<p dir="auto">Define "a long time".  It should take "very little" time.</p>
<p dir="auto">What here is taking much time?  If you want fastest, why do you go through your existing model at the <code>_model-&gt;data(_model-&gt;index(row,col))</code> level?  If you have access to the underlying actual model/data (like your <code>_dataList</code>) you can read from that directly to avoid the model indexing overhead.</p>
<p dir="auto">Nonetheless, I still would not expect your code to take long for "1000 rows and 15 columns".</p>
]]></description><link>https://forum.qt.io/post/729805</link><guid isPermaLink="true">https://forum.qt.io/post/729805</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 22 Sep 2022 13:20:21 GMT</pubDate></item><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 12:06:05 GMT]]></title><description><![CDATA[<p dir="auto">Hello again,<br />
As you said, I converted the model to QAbstractTableModel and used 2D QVector as source list ( data and header data).<br />
<strong>new code :</strong></p>
<pre><code>myModel::myModel(QVector&lt;QVector&lt;QString&gt; &gt; *dataList,QVector&lt;QString&gt; headerDataList, QObject *parent,int rowcount,int colcount) :
    QAbstractTableModel{parent},_dataList(dataList),_headerDataList(headerDataList),_rowCount(rowcount),_colCount(colcount)
{




}

QVariant myModel::data(const QModelIndex &amp;index, int role) const
{
    if (!index.isValid())
           return QVariant();
    if(index.row()&gt;_dataList-&gt;at(0).count()-1 || index.column()&gt;_dataList-&gt;count()-1)
        return QVariant();

    switch (role) {
    case Qt::DisplayRole:
        return _dataList-&gt;at(index.column()).at(index.row());

    case Qt::TextAlignmentRole:
        return Qt::AlignCenter;
    }
    return QVariant();

}



int myModel::rowCount(const QModelIndex &amp;parent) const
{

    return _dataList-&gt;at(0).count();
}

int myModel::columnCount(const QModelIndex &amp;parent) const
{

    return _dataList-&gt;count();
}

QVariant myModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal)
    {
        if(role == Qt::DisplayRole)
        {
            return _headerDataList.at(section);

        }
   }

    return QVariant();
}
</code></pre>
<p dir="auto">But I'm asking for better speed.<br />
For example, a model with 1000 rows and 15 columns takes a long time to load into these vectors. Is there a more convenient or faster method for this?</p>
<pre><code>
   _dataList = new QVector&lt;QVector&lt;QString&gt; &gt;;
   QVector&lt;QString&gt; _headerDataList;
   rowcount = _model-&gt;rowCount(); // this _model is another normal QSqlQueryModel.
   colcount = _model-&gt;columnCount();    
    
    for(int col = 0; col&lt;colcount; col++)
       {  
           QVector&lt;QString&gt; columnList;
           for(int row = 0; row &lt;rowcount ; row++)
           {
                columnList.append(_model-&gt;data(_model-&gt;index(row,col)).toString());
               
           }
           _dataList-&gt;append(columnList);
           _headerDataList.append(_model-&gt;headerData(col,Qt::Horizontal).toString());
       }
</code></pre>
]]></description><link>https://forum.qt.io/post/729789</link><guid isPermaLink="true">https://forum.qt.io/post/729789</guid><dc:creator><![CDATA[Nevez]]></dc:creator><pubDate>Thu, 22 Sep 2022 12:06:05 GMT</pubDate></item><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 07:23:06 GMT]]></title><description><![CDATA[<p dir="auto">QSqlQueryModel is designed to make database access while you do not do that at all.</p>
<p dir="auto">It's like taking the frame of an SUV to use it as a go-kart.</p>
<p dir="auto">Use the right tool for your purpose and in your case QAbstractTableModel is the one that makes sense.</p>
]]></description><link>https://forum.qt.io/post/729718</link><guid isPermaLink="true">https://forum.qt.io/post/729718</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 22 Sep 2022 07:23:06 GMT</pubDate></item><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 07:12:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said in <a href="/post/729709">subclass queryModel</a>:</p>
<blockquote>
<p dir="auto">Hi,<br />
First thing: why are you subclassing QSqlQueryModel when you want to show a custom data structure ? QAbstractTableModel would make more sense.</p>
</blockquote>
<p dir="auto">why do you suggest me to use QAbstractTableModel? what is the difference of this ?<br />
Thanks, I'll look into the other things you said.</p>
]]></description><link>https://forum.qt.io/post/729712</link><guid isPermaLink="true">https://forum.qt.io/post/729712</guid><dc:creator><![CDATA[Nevez]]></dc:creator><pubDate>Thu, 22 Sep 2022 07:12:52 GMT</pubDate></item><item><title><![CDATA[Reply to subclass queryModel on Thu, 22 Sep 2022 06:57:59 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">First thing: why are you subclassing QSqlQueryModel when you want to show a custom data structure ? <a href="https://doc.qt.io/qt-6/qabstracttablemodel.html" target="_blank" rel="noopener noreferrer nofollow ugc">QAbstractTableModel</a> would make more sense.<br />
Then, why not use something like <code>QVector&lt;QStringList&gt;</code> or even <code>QVector&lt;QVector&lt;QString&gt;&gt;</code> so you can more easily manipulate your data ? That would also allow you to have row and column count determined by the actual size of your 2D data rather than on fixed values.</p>
]]></description><link>https://forum.qt.io/post/729709</link><guid isPermaLink="true">https://forum.qt.io/post/729709</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 22 Sep 2022 06:57:59 GMT</pubDate></item></channel></rss>