<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body>
<p>Hi,</p>
<p>I had this problem here in Brazil, too, but with Qt 5.0, perhaps that's the same point.</p>
<p>To overcome this problem, I found two solutions:</p>
<p>1 (easier): Do you really have to use ODBC? Native drivers are supposedly better and faster. Try using the native driver for your DB.</p>
<p>2 (not so easy): Go to the QODBC source file and replace every ocurrence of:</p>
<p><span style="font-family: 'courier new', courier;">QString::fromUtf8 -> QString::fromLocal8Bit<br />QString::toUtf8 -> QString::toLocal8Bit</span></p>
<p> </p>
<p>I believe the sources are located in <span style="font-family: 'courier new', courier;">$QT_SRC/qtbase/src/sql/drivers/odbc/</span></p>
<p>At least, that solved my problem, before I switched to native driver (QOCI - no problems with it).</p>
<p><span>Hope it works. Best,</span><br /><span>Edgar.</span></p>
<p>Em 26/08/2014 10:14, zdenko podobny escreveu:</p>
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px"><!-- html ignored --><!-- head ignored --><!-- meta ignored -->
<div dir="ltr">Hello,
<div> </div>
<div>I try to retrieve strings from db with <span style="color: #008000;">QODBC </span>(QT 5.3.1 windows).</div>
<div>Database locale is en_US.819 (and ODBC driver does not work if client locale is not set to en_US.819). Strings (stored by windows application in db) are cp1250 encoded.</div>
<div> </div>
<div>And instead of "<span style="color: #aa0000; font-family: Courier; font-size: 10pt;">doručené</span>" I got "<span style="color: #aa0000; font-family: Courier; font-size: 10pt;">doru?en?</span>".</div>
<div> </div>
<div>Here is part of my code:</div>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">  </span><span style="color: #800080;">QTextCodec</span><span style="color: #000000;">::</span><span style="color: #000000;">setCodecForLocale</span><span style="color: #000000;">(</span><span style="color: #800080;">QTextCodec</span><span style="color: #000000;">::</span><span style="color: #000000;">codecForName</span><span style="color: #000000;">(</span><span style="color: #008000;">"cp1250"</span><span style="color: #000000;">));</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">  </span><span style="color: #800080;">QTextCodec</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">*</span><span style="color: #000000;">codec_db</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">=</span><span style="color: #c0c0c0;"> </span><span style="color: #800080;">QTextCodec</span><span style="color: #000000;">::</span><span style="color: #000000;">codecForName</span><span style="color: #000000;">(</span><span style="color: #008000;">"latin1"</span><span style="color: #000000;">);</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">  </span><span style="color: #800080;">QSqlRecord</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">rec</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">=</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">query</span><span style="color: #000000;">.</span><span style="color: #000000;">record</span><span style="color: #000000;">();</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">  </span><span style="color: #000080;">qDebug</span><span style="color: #000000;">()</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;"><<</span><span style="color: #c0c0c0;"> </span><span style="color: #008000;">"test:</span><span style="color: #c0c0c0;"> </span><span style="color: #008000;">"</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;"><<</span><span style="color: #c0c0c0;"> </span><span style="color: #008000;">"doručené"</span><span style="color: #000000;">;</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">  </span><span style="color: #808000;">while</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">(</span><span style="color: #000000;">query</span><span style="color: #000000;">.</span><span style="color: #000000;">next</span><span style="color: #000000;">())</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">{</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">    </span><span style="color: #808000;">for</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">(</span><span style="color: #808000;">int</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">x</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">=</span><span style="color: #c0c0c0;"> </span><span style="color: #000080;">0</span><span style="color: #000000;">;</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">x</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;"><</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">rec</span><span style="color: #000000;">.</span><span style="color: #000000;">count</span><span style="color: #000000;">();</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">x</span><span style="color: #000000;">++)</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">{</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">      </span><span style="color: #800080;">QVariant</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">variant</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">=</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">query</span><span style="color: #000000;">.</span><span style="color: #000000;">value</span><span style="color: #000000;">(</span><span style="color: #000000;">x</span><span style="color: #000000;">);</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">      </span><span style="color: #808000;">if</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">(</span><span style="color: #000000;">rec</span><span style="color: #000000;">.</span><span style="color: #000000;">value</span><span style="color: #000000;">(</span><span style="color: #000000;">x</span><span style="color: #000000;">).</span><span style="color: #000000;">type</span><span style="color: #000000;">()</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">==</span><span style="color: #c0c0c0;"> </span><span style="color: #800080;">QMetaType</span><span style="color: #000000;">::</span><span style="color: #800080;">QString</span><span style="color: #000000;">)</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">{</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">        </span><span style="color: #800080;">QByteArray</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">encodedString</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">=</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">codec_db</span><span style="color: #000000;">-></span><span style="color: #000000;">fromUnicode</span><span style="color: #000000;">(</span><span style="color: #000000;">variant</span><span style="color: #000000;">.</span><span style="color: #000000;">toByteArray</span><span style="color: #000000;">());</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">        </span><span style="color: #000080;">qDebug</span><span style="color: #000000;">()</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;"><<</span><span style="color: #c0c0c0;"> </span><span style="color: #008000;">"db_str:</span><span style="color: #c0c0c0;"> </span><span style="color: #008000;">"</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;"><<</span><span style="color: #c0c0c0;"> </span><span style="color: #000000;">encodedString</span><span style="color: #000000;">;</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">      </span><span style="color: #000000;">}</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">    </span><span style="color: #000000;">}</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #c0c0c0;">  </span><span style="color: #000000;">}</span></pre>
</div>
<div>How can I fix encoding of db output?</div>
<div> </div>
<div>PS: In python 3.4.1 I am able to get correct output (via pyodbc) with this:</div>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="color: #808000;">    print</span><span style="color: #000000;">(</span><span style="color: #008000;">"</span><span style="color: #008000; font-family: arial;">db_str</span><span style="font-family: arial; color: #008000;">:"</span><span style="font-family: arial; color: #000000;">,</span><span style="font-family: arial; color: #c0c0c0;"> </span><span style="font-family: Courier; font-size: 10pt; color: #000000;">db_str</span><span style="font-family: arial; color: #000000;">.</span><span style="font-family: Courier; font-size: 10pt; color: #000000;">encode</span><span style="font-family: arial; color: #000000;">(</span><span style="font-family: arial; color: #008000;">'latin1'</span><span style="font-family: arial; color: #000000;">).</span><span style="font-family: Courier; font-size: 10pt; color: #000000;">decode</span><span style="font-family: arial; color: #000000;">(</span><span style="font-family: arial; color: #008000;">'cp1250'</span><span style="font-family: arial; color: #000000;">))</span></pre>
</div>
<div>But I would prefer to use C++ and QT5...</div>
<div> </div>
<div>
<div>Zdenko</div>
<input type="hidden" name="virtru-metadata" value="{"email-policy":{"state":"closed","expirationUnit":"days","disableCopyPaste":false,"disablePrint":false,"disableForwarding":false,"expires":false},"attachments":{}}" /></div>
</div>
<br />
<pre>_______________________________________________
Interest mailing list
<a href="mailto:Interest@qt-project.org">Interest@qt-project.org</a>
<a href="http://lists.qt-project.org/mailman/listinfo/interest">http://lists.qt-project.org/mailman/listinfo/interest</a>
</pre>
</blockquote>
<p> </p>
<div>-- <br />
<p><span style="font-family: 'courier new', courier; color: #003300;">/**<br /></span><span style="font-family: 'courier new', courier; color: #003300;"> * @<strong>author</strong> Edgar Moraes Diniz<br /> *<br /></span><span style="font-family: 'courier new', courier; color: #003300;"> * edgar.diniz@openmailbox.org<br /></span><span style="font-family: 'courier new', courier; color: #003300;"> */</span></p>
</div>
</body></html>