<?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 in QML get data with QSqlQueryModel]]></title><description><![CDATA[<p dir="auto">Hi!<br />
I have model QSqlQueryModel.<br />
Me need get data with model in QML.<br />
This code not work:</p>
<pre><code>console.log("printFormatModel: " + printFormatModel.data(printFormatModel.index(1,1),1))
</code></pre>
<p dir="auto">Debug return:</p>
<pre><code>qml: printFormatModel: undefined
</code></pre>
<p dir="auto">С++ have metod toInt(), but QML not have this metod.</p>
]]></description><link>https://forum.qt.io/topic/107308/how-in-qml-get-data-with-qsqlquerymodel</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 14:48:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/107308.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Sep 2019 07:49:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Tue, 01 Oct 2019 18:39:11 GMT]]></title><description><![CDATA[<p dir="auto">I did not find the necessary information in the documentation. I want to get data from the model from the cell I want.</p>
]]></description><link>https://forum.qt.io/post/553852</link><guid isPermaLink="true">https://forum.qt.io/post/553852</guid><dc:creator><![CDATA[Mihaill]]></dc:creator><pubDate>Tue, 01 Oct 2019 18:39:11 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 22:00:10 GMT]]></title><description><![CDATA[<p dir="auto">New thing named "read docs"? It is your code, use debugger, we do not know what are you trying to achieve.</p>
]]></description><link>https://forum.qt.io/post/553689</link><guid isPermaLink="true">https://forum.qt.io/post/553689</guid><dc:creator><![CDATA[IntruderExcluder]]></dc:creator><pubDate>Mon, 30 Sep 2019 22:00:10 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 20:36:05 GMT]]></title><description><![CDATA[<p dir="auto">So what to do?</p>
]]></description><link>https://forum.qt.io/post/553681</link><guid isPermaLink="true">https://forum.qt.io/post/553681</guid><dc:creator><![CDATA[Mihaill]]></dc:creator><pubDate>Mon, 30 Sep 2019 20:36:05 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 18:58:01 GMT]]></title><description><![CDATA[<p dir="auto">You cannot write</p>
<pre><code>printFormatModel.data(printFormatModel.index(1,1),"idRole")
</code></pre>
<p dir="auto">Second parametr in this case for method <code>data</code> would be <code>"idRole"</code> which is string, while model waits for some integer value. When you use it like</p>
<pre><code>printFormatModel.data(printFormatModel.index(1,1),1)
</code></pre>
<p dir="auto">Your second parameter would be <code>1</code>. But according to your code <code>modelIndex</code> at <code>PrintFormatModel::data</code> would be invalid, since <code>Qt::UserRole</code> is equal to 256, you will get <code>1 - 256 - 1</code> for <code>columnId</code>.</p>
]]></description><link>https://forum.qt.io/post/553667</link><guid isPermaLink="true">https://forum.qt.io/post/553667</guid><dc:creator><![CDATA[IntruderExcluder]]></dc:creator><pubDate>Mon, 30 Sep 2019 18:58:01 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 16:58:28 GMT]]></title><description><![CDATA[<p dir="auto">I do not understand you. I tried to point out the numbers, but it did not help me.</p>
]]></description><link>https://forum.qt.io/post/553646</link><guid isPermaLink="true">https://forum.qt.io/post/553646</guid><dc:creator><![CDATA[Mihaill]]></dc:creator><pubDate>Mon, 30 Sep 2019 16:58:28 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 16:48:20 GMT]]></title><description><![CDATA[<p dir="auto">Role parameter should be integer, not a string. Look at method definition. You can at least try to print what <code>id</code> you get and result of your <code>columnId</code>, make sure they are valid.</p>
]]></description><link>https://forum.qt.io/post/553642</link><guid isPermaLink="true">https://forum.qt.io/post/553642</guid><dc:creator><![CDATA[IntruderExcluder]]></dc:creator><pubDate>Mon, 30 Sep 2019 16:48:20 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 16:20:39 GMT]]></title><description><![CDATA[<p dir="auto">It's too not works:</p>
<pre><code>printFormatModel.data(printFormatModel.index(1,1),"idRole")
</code></pre>
<p dir="auto">It's my model:</p>
<pre><code>#include "printformatmodel.h"

PrintFormatModel::PrintFormatModel(QObject *parent) : QSqlQueryModel (parent)
{

}

QVariant PrintFormatModel::data(const QModelIndex &amp;index, int role) const
{
    int columnId = role - Qt::UserRole - 1;
    // Создаём индекс с помощью новоиспечённого ID колонки
    QModelIndex modelIndex = this-&gt;index(index.row(), columnId);

    /* И с помощью уже метода data() базового класса
     * вытаскиваем данные для таблицы из модели
     * */
    return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}

QVariantMap PrintFormatModel::get(int idx) const
{
    QVariantMap map;
    foreach(int k, roleNames().keys()) {
        map[roleNames().value(k)] = data(index(idx, 0), k);
    }
    return map;
}

QHash&lt;int, QByteArray&gt; PrintFormatModel::roleNames() const
{
    QHash&lt;int, QByteArray&gt; roles;
    roles[idRole]="id";
    roles[productIdColumn]="product_id";
    roles[printFormatId]="print_format_id";
    roles[x1]="x1";
    roles[y1]="y1";
    roles[fontSize]="font_size";
    roles[printFormatKey]="print_format_key";
    roles[printFormatText]="print_format_text";
    roles[boolPrintFormatKey1]="bool_print_format_key_1";
    roles[boolPrintFormatKey2]="bool_print_format_key_2";
    roles[boolPrintFormatKey3]="bool_print_format_key_3";
    roles[boolPrintFormatKey4]="bool_print_format_key_4";
    roles[boolPrintFormatKey1Text]="bool_print_format_key_4_text";
    return roles;
}

void PrintFormatModel::setId(int idNumber)
{
    productId = idNumber;
}

void PrintFormatModel::updateModel(int id)
{
    this-&gt;setQuery("select id, product_id, print_format_id, x1, y1, font_size, print_format_key,"
                   " print_format_text, bool_print_format_key_1, bool_print_format_key_2,"
                   " bool_print_format_key_3, bool_print_format_key_4, bool_print_format_key_4_text"
                   " from ProductConfigForPrint WHERE product_id = " + QString::number(id));
}

int PrintFormatModel::getId(int row)
{
    return this-&gt;data(this-&gt;index(row, 0), idRole).toInt();
}

</code></pre>
]]></description><link>https://forum.qt.io/post/553634</link><guid isPermaLink="true">https://forum.qt.io/post/553634</guid><dc:creator><![CDATA[Mihaill]]></dc:creator><pubDate>Mon, 30 Sep 2019 16:20:39 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 10:05:28 GMT]]></title><description><![CDATA[<p dir="auto">Second parameter of <code>data</code> method is model <code>role</code>. According to your example you are trying to get <code>DecorationRole</code>. Maybe role is simply set wrong?</p>
]]></description><link>https://forum.qt.io/post/553575</link><guid isPermaLink="true">https://forum.qt.io/post/553575</guid><dc:creator><![CDATA[IntruderExcluder]]></dc:creator><pubDate>Mon, 30 Sep 2019 10:05:28 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 09:43:12 GMT]]></title><description><![CDATA[<p dir="auto">The example is not what I need. I need to go through the entire model and get the data from the right cells. The model is reflected only in the Listview, not all columns of the model are reflected.</p>
]]></description><link>https://forum.qt.io/post/553569</link><guid isPermaLink="true">https://forum.qt.io/post/553569</guid><dc:creator><![CDATA[Mihaill]]></dc:creator><pubDate>Mon, 30 Sep 2019 09:43:12 GMT</pubDate></item><item><title><![CDATA[Reply to How in QML get data with QSqlQueryModel on Mon, 30 Sep 2019 09:23:35 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">It's explained in the <a href="https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#" target="_blank" rel="noopener noreferrer nofollow ugc">Using C++ Models with Qt Quick Views chapter of Qt's documentation</a>.</p>
]]></description><link>https://forum.qt.io/post/553563</link><guid isPermaLink="true">https://forum.qt.io/post/553563</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 30 Sep 2019 09:23:35 GMT</pubDate></item></channel></rss>