PDA

View Full Version : std formatted string with QTextEdit



nileshsince1980
10th February 2010, 05:04
Hi All,
In my QT application ( QT 4.5.1 & Windows XP , SUSE Linux) I am formatting the string using std formatting functions (e.g std::setw() )
But when I fomratted required string and put it on QTextEdit to display the formatting doesnt get apply But if same string I see using std::cout I see the formatting is correctly applied. Can you tell me how to apply formatting in QTextEdit.

e.g.
std::stringstream ss;
ss << std::left << std::setw(40) << "Values" << std::setw(40) << "Keyword/Constant" << std::endl;
ss << std::left << std::setw(40) << "One" << std::setw(40) << "One" << std::endl;
ss << std::left << std::setw(40) << "Four" << std::setw(40) << "Four" << std::endl;
ss << std::left << std::setw(40) << "Three" << std::setw(40) << "Three" << std::endl;

cout << ss.str(); //Works fine

textEdit->setPlainText(QString::fromStdString(ss.str())); // Formatting doesnt apply

Ginsengelf
10th February 2010, 06:58
Hi, is there a special reason why you have to use std::stringstream? Otherwise you should use QString's methods like QString::arg().

Ginsengelf

nileshsince1980
10th February 2010, 08:20
Hi,
Thanks for reply ...
As I want to use std::setw(..) function for formatting and return the std::string object which will be then covert to QString using fromStdString(..).
Can you tell me how to use QString::arg(..) function for above code??

Thanks in advance !

gargoylle_ltk
10th February 2010, 10:16
What exactly happens ? Does it help if you set a fixed-width font (such as courier new) for the QTextEdit ?

nileshsince1980
10th February 2010, 11:41
Hi,
In my app, it is not setting the fixed width font with QTextEdit so I am not able to get the output as I could see using std::cout.
If you tried to test above code you will see the difference between the expected output & actualt output.

pitonyak
10th February 2010, 14:24
Hi,
Thanks for reply ...
As I want to use std::setw(..) function for formatting and return the std::string object which will be then covert to QString using fromStdString(..).
Can you tell me how to use QString::arg(..) function for above code??

Check the docs! http://qt.nokia.com/doc/4.6/qstring.html#arg

Consider this code

ss << std::left << std::setw(40) << "Values" << std::setw(40) << "Keyword/Constant" << std::endl;
This will likely work with

QString("%1%2\n").arg("Values", -40).arg("Keyword/Constant", -40)
As a side note, if you desire to do this in a similar way using QT constructs, you can use a QTextSTream
http://qt.nokia.com/doc/4.6/qtextstream.html
Points of interest are:

setFieldAlignment Sets the field alignment mode (so you can set left, center, right...). Use the left() shortcut.
setFieldWidth Sets the current field width to width. This also applies to endl, so be careful.

OK, now for an untested example:

QString s;
QTextStream ss(&s);
ss.setFieldAlignment(QTextStream::AlignLeft);
ss.setFieldWidth(40);
...

You can also use the inline functions:

QString s;
QTextStream ss(&s);
ss << left << qSetFieldWidth(40) << "Value" << "Keyword/Constant" << qSetFieldWidth(0) << endl;
ss << qSetFieldWidth(40) << "One" << "One" << qSetFieldWidth(0) << endl;
qDebug(qPrintable(s));

I will note, however, that printing the last item on a line with a field width of 40 seems a little silly, because this simply provides spaces after the field, but before the new line character.

pitonyak
10th February 2010, 14:27
Hi,
In my app, it is not setting the fixed width font with QTextEdit so I am not able to get the output as I could see using std::cout.
If you tried to test above code you will see the difference between the expected output & actualt output.

Have you considered using tabs between columns rather than spaces? You can then use setTabStopWidth to set the tab width if it is not appropriate.

nileshsince1980
11th February 2010, 05:05
Hi Pitonyak,
thanks for reply.
I tried usinbg QTextStream class ..but still I am not able to see formatting as per expected for QTextEdit class.
If you tried the output on command window using cout or qDebug and in QTextEdit you will able to see the difference.
e.g. Using cout
Values Keyword/Constant
One One
Three Three
Four Four

but in QTextEdit class
Values Keyword/Constant
One One
Three Three
Four Four

The column width is not setting propertly for QTextWidth.

pitonyak
11th February 2010, 05:27
I do not have an example handy that allows me to check a column width... I just looked where I expected to see such a thing.

Lesiok
11th February 2010, 07:23
Remember that GUI is working with non constant character width font. Ie. word "mmmm" is about 3-4 times bigger than "iiii". Command line window, where You can see qDebug() or cout output is working with constant character width font. Ie. words "mmmm" are "iiii" have this same size. Just like here :
Text with non constant width font :
mmmm
iiii

and Qt Code (text with constant width font)
mmmm
iiii

gargoylle_ltk
12th February 2010, 16:38
I'm still not sure I understand exactly what the problem is but here is a screenshot of what I tried (you can see the actual code in the background):
4280
Is this not what you expected/wanted ?

nileshsince1980
24th February 2010, 08:21
Hi gargoylle_ltk

I want exactly as per the snap shot you have shown.
If if I use QTextEdit with constant width font.

nileshsince1980
24th February 2010, 08:23
Hi Lesiok

thanks for reply.
I think I have to use constand with font for QTextEdit to get the output expected.
Can you tell me how to use QTextEdit for constant width font ??

Thanks,
Nilesh

Lesiok
24th February 2010, 10:23
Look at QWidget::font (http://doc.trolltech.com/4.6/qwidget.html#font-prop)

nileshsince1980
25th February 2010, 06:46
Thanks to all,
I got the solution,

QFont fnt("",10);

fnt.setStyleHint(QFont::Courier, QFont::PreferQuality);

QTextEdit* ed = new QTextEdit();

ed->setCurrentFont(fnt);


Above code works fine for me,

Thanks again...!!!

Nilesh