PDA

View Full Version : German letter with QTextLine



cic1988
2nd September 2014, 10:27
Hello,

i found the QTextLine::setLineWidth() gave me different results between normal english letter and german letter

e.g.




QString string = str;
QString result;

QTextLayout layout(str, font);
QTextOption opt = layout.textOption();
opt.setWrapMode(QTextOption::WrapAtWordBoundaryOrA nywhere);

layout.setTextOption(opt);
layout.beginLayout();

QTextLine line = layout.createLine();

line.setLineWidth(width);
line.textLength(); // <---- *
}
layout.endLayout();


i use my application font to get the line width with string length * font width.

let's assume that i set the line width with 5 characters width.
with english letter the result from * gave me 5, but with german letter gave me 2.

what's the problem?

wysota
2nd September 2014, 10:56
Please prepare a minimal compilable example reproducing the problem.

cic1988
2nd September 2014, 11:35
thanks for the reply,

sure the test program is as followed.






// Font Info: Consolas,10,-1,2,50,0,0,0,1,0" via QFont::toString()

#include <QApplication>
#include <QtTest/qtest.h>

#include <QtGui>

int tst(const QString &str, const QFont &);

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QFont f("Consolas");
f.setPointSizeF(10);
f.setStyleHint((QFont::StyleHint)2);
f.setWeight(50);
f.setFixedPitch(1);

QString englishStr("abadjiownvneinvwAJOINVEajiJIinv");
int length = tst(englishStr, f); // <--- 5

QString germanStrSmall("öüäöüäöüääöüà ¶");
length = tst(germanStrSmall, f); // <--- 4

QString germanStrBig("ÜÄÖÖüääöüäüà¤ÃƒÂ¶");
length = tst(germanStrBig, f); // <--- 8

QString germanStrBigAndSmall("ÜäüÄÖÖüääöüà¤ÃƒÂ¼ÃƒÂ¤ÃƒÂ¶");
length = tst(germanStrBigAndSmall, f); // <--- 6

return a.exec();
}

int tst(const QString& str, const QFont& f)
{
QFontMetrics fm(f);

// for test with 5 characters' width
int textWidth = 5 * fm.width(QChar::Nbsp);

QTextLayout layout(str, f);
QTextOption opt = layout.textOption();
opt.setWrapMode(QTextOption::WrapAtWordBoundaryOrA nywhere);

layout.setTextOption(opt);
layout.beginLayout();

QTextLine line = layout.createLine();
line.setLineWidth(textWidth);
layout.endLayout();

return line.textLength();
}

wysota
2nd September 2014, 11:41
I get totally different values than you do (5, 5, 4, 5). Are you sure text encoding is correct? When I wrap the strings into QString::fromUtf8(), I'm getting different results (all values are 5). What is the expected result?

cic1988
2nd September 2014, 11:50
it is strange... i have no clue why you got different result tham i did..

the expected result should be all 5.

I think the problem comes from my program's text encoding. Thanks for your help.