<?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[Build QImage from postgresql database via libpqxx]]></title><description><![CDATA[<p dir="auto">Hello. colleagues !</p>
<p dir="auto">I have to build QImage object from bytea field of postgresql database, previously I do it using pqxx::binarystring as described in theme in <a href="https://forum.qt.io/topic/51827/solved-how-to-save-an-image-into-a-database/2">here</a>, but when pqxx::binarystring became deprecated and I have to use std::basic_string&lt;std::byte&gt;  type instead, I have got invalid image with this, debug shows that binary string became different. Is it problem with libpqxx or Qt ?</p>
]]></description><link>https://forum.qt.io/topic/142594/build-qimage-from-postgresql-database-via-libpqxx</link><generator>RSS for Node</generator><lastBuildDate>Mon, 06 Jul 2026 14:24:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/142594.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 01 Feb 2023 09:25:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Build QImage from postgresql database via libpqxx on Wed, 01 Feb 2023 17:40:52 GMT]]></title><description><![CDATA[<p dir="auto">Thanks a lot, problem was solved, using libpqxx method pqxx::field::as&lt; std::basic_string &lt;std::byte&gt; &gt;();</p>
<pre><code>std::basic_string&lt;std::byte&gt; CVDbPgResult::getCellAsBinaryString( int row, int column) const {
    if( m_res == nullptr || row &gt;= m_res-&gt;size() || column &gt;= m_res-&gt;at(row).size() )
        return std::basic_string&lt;std::byte&gt;(nullptr, 0);

    pqxx::field fCell( m_res-&gt;at(row).at(column) );
    return fCell.as&lt; std::basic_string&lt;std::byte&gt; &gt;();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/745894</link><guid isPermaLink="true">https://forum.qt.io/post/745894</guid><dc:creator><![CDATA[YuriyRusinov]]></dc:creator><pubDate>Wed, 01 Feb 2023 17:40:52 GMT</pubDate></item><item><title><![CDATA[Reply to Build QImage from postgresql database via libpqxx on Wed, 01 Feb 2023 11:52:48 GMT]]></title><description><![CDATA[<p dir="auto">Therefore I need to use something like this vector&lt;std::byte&gt; or vector&lt;char&gt; ?</p>
]]></description><link>https://forum.qt.io/post/745830</link><guid isPermaLink="true">https://forum.qt.io/post/745830</guid><dc:creator><![CDATA[YuriyRusinov]]></dc:creator><pubDate>Wed, 01 Feb 2023 11:52:48 GMT</pubDate></item><item><title><![CDATA[Reply to Build QImage from postgresql database via libpqxx on Wed, 01 Feb 2023 09:44:16 GMT]]></title><description><![CDATA[<p dir="auto">I mean - it's clearly <a href="https://libpqxx.readthedocs.io/en/6.1.1/a00807.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">stated</a> and obvious: So don't assume that it can be treated as a C-style string unless you've made sure of this yourself.</p>
<p dir="auto">Use a proper container like e.g. std::vector&lt;&gt;</p>
]]></description><link>https://forum.qt.io/post/745815</link><guid isPermaLink="true">https://forum.qt.io/post/745815</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 01 Feb 2023 09:44:16 GMT</pubDate></item><item><title><![CDATA[Reply to Build QImage from postgresql database via libpqxx on Wed, 01 Feb 2023 09:40:45 GMT]]></title><description><![CDATA[<p dir="auto">Old code</p>
<pre><code>
pqxx::binarystring CVDbPgResult::getCellAsBinaryString0( int row, int column ) const {
    if( m_res == nullptr || row &gt;= m_res-&gt;size() || column &gt;= m_res-&gt;at(row).size() )
        return pqxx::binarystring(nullptr, 0);

    pqxx::field fCell( m_res-&gt;at(row).at(column) );
    pqxx::binarystring fCellB( fCell );
    return fCellB;
}

    int nn = getCellLength( row, column );
    pqxx::binarystring imBytesStr0 = getCellAsBinaryString0( row, column );
    const char* imBytes0 = imBytesStr0.get();
    QByteArray imABytes = QByteArray::fromRawData( imBytes0, nn );
    QImage resImage;
    bool isLoaded = resImage.loadFromData( imABytes );

</code></pre>
<p dir="auto">New code:</p>
<pre><code>    basic_string&lt;std::byte&gt; imBytesStr = getCellAsBinaryString( row, column );
    const void* imV = static_cast&lt;const void*&gt;(imBytesStr.data());
    const char* imBytes = static_cast&lt;const char *&gt;(imV);

std::basic_string&lt;std::byte&gt; CVDbPgResult::getCellAsBinaryString( int row, int column) const {
    if( m_res == nullptr || row &gt;= m_res-&gt;size() || column &gt;= m_res-&gt;at(row).size() )
        return std::basic_string&lt;std::byte&gt;(nullptr, 0);

    pqxx::field fCell( m_res-&gt;at(row).at(column) );
    pqxx::binarystring fCellB( fCell );
    int nn = fCell.size();
    const char* data = fCell.c_str();
    qDebug() &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; nn &lt;&lt; strlen(data) &lt;&lt; fCellB.size();
    const void* vData = static_cast&lt;const void *&gt;(data);

    const std::byte* vres(static_cast&lt;const std::byte*&gt;(vData));
    // (data, nn);//get&lt; std::string &gt;();
    std::basic_string&lt;std::byte&gt; res(vres);
    return res;
}

</code></pre>
]]></description><link>https://forum.qt.io/post/745814</link><guid isPermaLink="true">https://forum.qt.io/post/745814</guid><dc:creator><![CDATA[YuriyRusinov]]></dc:creator><pubDate>Wed, 01 Feb 2023 09:40:45 GMT</pubDate></item><item><title><![CDATA[Reply to Build QImage from postgresql database via libpqxx on Wed, 01 Feb 2023 09:28:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/yuriyrusinov">@<bdi>YuriyRusinov</bdi></a> Please show your code.</p>
]]></description><link>https://forum.qt.io/post/745810</link><guid isPermaLink="true">https://forum.qt.io/post/745810</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 01 Feb 2023 09:28:43 GMT</pubDate></item></channel></rss>