Results 1 to 15 of 15

Thread: std formatted string with QTextEdit

  1. #1
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default std formatted string with QTextEdit

    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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    Hi, is there a special reason why you have to use std::stringstream? Otherwise you should use QString's methods like QString::arg().

    Ginsengelf

  3. #3
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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 !

  4. #4
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    What exactly happens ? Does it help if you set a fixed-width font (such as courier new) for the QTextEdit ?

  5. #5
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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.

  6. #6
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    Quote Originally Posted by nileshsince1980 View Post
    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
    Qt Code:
    1. ss << std::left << std::setw(40) << "Values" << std::setw(40) << "Keyword/Constant" << std::endl;
    To copy to clipboard, switch view to plain text mode 
    This will likely work with
    Qt Code:
    1. QString("%1%2\n").arg("Values", -40).arg("Keyword/Constant", -40)
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QTextStream ss(&s);
    2. ss.setFieldAlignment(QTextStream::AlignLeft);
    3. ss.setFieldWidth(40);
    4. ...
    To copy to clipboard, switch view to plain text mode 
    You can also use the inline functions:
    Qt Code:
    1. QTextStream ss(&s);
    2. ss << left << qSetFieldWidth(40) << "Value" << "Keyword/Constant" << qSetFieldWidth(0) << endl;
    3. ss << qSetFieldWidth(40) << "One" << "One" << qSetFieldWidth(0) << endl;
    4. qDebug(qPrintable(s));
    To copy to clipboard, switch view to plain text mode 
    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.

  7. #7
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    Quote Originally Posted by nileshsince1980 View Post
    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.

  8. #8
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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.

  9. #9
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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.

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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)
    Qt Code:
    1. mmmm
    2. iiii
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to Lesiok for this useful post:

    nileshsince1980 (24th February 2010)

  12. #11
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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):
    snapshot1.jpg
    Is this not what you expected/wanted ?

  13. The following user says thank you to gargoylle_ltk for this useful post:

    nileshsince1980 (24th February 2010)

  14. #12
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    Hi gargoylle_ltk

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

  15. #13
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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

  16. #14
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    Look at QWidget::font

  17. The following user says thank you to Lesiok for this useful post:

    nileshsince1980 (25th February 2010)

  18. #15
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std formatted string with QTextEdit

    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

Similar Threads

  1. Replies: 4
    Last Post: 21st June 2009, 18:06
  2. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  3. Drawing Rich Formatted Text with QPainter
    By millsks in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 19:59
  4. Int to String - manipulating string
    By mickey in forum General Programming
    Replies: 6
    Last Post: 5th November 2007, 20:11
  5. formatted text in QTreeWidgetItem
    By swiety in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2007, 16:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.