Results 1 to 10 of 10

Thread: Formatting a QString

  1. #1
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Formatting a QString

    A QString text should be displayed with differen colors and fonts.
    It seems that coloring QString is not possible - like in other classes, e.g. QTextEdit (QTextEdit::setTextColor). How can I convert the QString to another class ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Formatting a QString

    QString just holds a string, it doesn't have any appearance. Where do you want to display that string?

  3. #3
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Formatting a QString

    It's in a QTextEdit panel.
    Currently the text is appended thru "tr(stringtext.toAscii().data())".
    But this way there is no formatting possibility.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Formatting a QString

    Quote Originally Posted by icentenee
    But this way there is no formatting possibility.
    You can add rich text tags after it was translated.

    Another possibility is to use QSyntaxHighlighter.

  5. #5
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Formatting a QString

    How can I set a rich tag to a QString ?
    "tr(stringtext.toAscii().data())" is a QString, and is put to the QTextEdit panel
    with append - may be the attributes can be changed afterwards, when the text
    is written out (inside the QTextEdit instance) ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Formatting a QString

    Quote Originally Posted by icentenee
    How can I set a rich tag to a QString ?
    Something like this should work:
    Qt Code:
    1. QString msg( tr(...) );
    2. QString format( "<span style=\"...\">%1</span>" );
    3. ...
    4. QString formattedMsg( format.arg( msg ) );
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by icentenee
    may be the attributes can be changed afterwards, when the text
    is written out (inside the QTextEdit instance) ?
    You should be able to change them through QTextDocument and QTextFormat.

  7. #7
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Formatting a QString

    this try:
    ---------
    QTextEditor txtEditor;
    QString format( "<span style=\"style=color:#FF0000\">%1</span>" );
    QString formattedMsg( format.arg(result_all) );
    txtEditor.append( formattedMsg.toAscii().data() );

    HTML is resolved, but there is no text formatting (coloring).
    Seems that the string is simply output as ascii.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Formatting a QString

    Quote Originally Posted by icentenee View Post
    "<span style=\"style=color:#FF0000\">%1</span>"
    Shouldn't it be: "<span style=\"color:#FF0000\">%1</span>"?

  9. #9
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Formatting a QString

    Sorry, ok now, that was an edit error.
    There is another possibility, with the QTextCursor class, I assume, for formatting
    text sections.
    Do you know about examples ?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Formatting a QString

    Qt Code:
    1. include <QApplication>
    2. #include <QPushButton>
    3. #include <QTextCharFormat>
    4. #include <QTextCursor>
    5. #include <QTextEdit>
    6. #include <QVBoxLayout>
    7. #include <QWidget>
    8.  
    9. class Test : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. Test() : QWidget( 0 )
    14. {
    15. _textEdit = new QTextEdit();
    16. QPushButton *b = new QPushButton( "Format" );
    17.  
    18. l->addWidget( _textEdit );
    19. l->addWidget( b );
    20. setLayout( l );
    21.  
    22. connect( b, SIGNAL( clicked() ), this, SLOT( format() ) );
    23. }
    24.  
    25. private slots:
    26. void format()
    27. {
    28. QTextCursor cur( _textEdit->document() );
    29.  
    30. // indent first paragraph
    31. QTextBlockFormat f( cur.blockFormat() ); // note: you get a copy of the format
    32. f.setIndent( 1 );
    33. cur.setBlockFormat( f ); // here you replace the format used in document
    34.  
    35. // move to next paragraph
    36. if( cur.movePosition( QTextCursor::NextBlock ) ) {
    37.  
    38. // select first line
    39. cur.movePosition( QTextCursor::StartOfLine );
    40. cur.movePosition( QTextCursor::EndOfLine, QTextCursor::KeepAnchor );
    41. // note that what "line" means depends on the size of text edit --- resize the
    42. // window to see how it behaves
    43.  
    44. // underline selected line
    45. QTextCharFormat f( cur.charFormat() );
    46. f.setFontUnderline( true );
    47. cur.setCharFormat( f );
    48. }
    49. }
    50.  
    51. private:
    52. QTextEdit *_textEdit;
    53. };
    54.  
    55. int main( int argc, char **argv )
    56. {
    57. QApplication app( argc, argv );
    58.  
    59. Test t;
    60. t.show();
    61.  
    62. return app.exec();
    63. }
    64.  
    65. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

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

    icentenee (15th October 2006)

Similar Threads

  1. Carriage Return in QString
    By incapacitant in forum Newbie
    Replies: 7
    Last Post: 2nd December 2010, 09:18
  2. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. QString ~ QByte error
    By mhoover in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2006, 20:01
  5. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52

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.