View Full Version : Formatting a QString
icentenee
1st September 2006, 16:05
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 ?
jacek
1st September 2006, 16:34
QString just holds a string, it doesn't have any appearance. Where do you want to display that string?
icentenee
1st September 2006, 17:12
It's in a QTextEdit panel.
Currently the text is appended thru "tr(stringtext.toAscii().data())".
But this way there is no formatting possibility.
jacek
1st September 2006, 17:20
But this way there is no formatting possibility.
You can add rich text tags after it was translated.
Another possibility is to use QSyntaxHighlighter.
icentenee
3rd September 2006, 12:03
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) ?
jacek
3rd September 2006, 14:33
How can I set a rich tag to a QString ?
Something like this should work:
QString msg( tr(...) );
QString format( "<span style=\"...\">%1</span>" );
...
QString formattedMsg( format.arg( msg ) );
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.
icentenee
28th September 2006, 21:29
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.
jacek
28th September 2006, 22:06
"<span style=\"style=color:#FF0000\">%1</span>"
Shouldn't it be: "<span style=\"color:#FF0000\">%1</span>"?
icentenee
1st October 2006, 11:00
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 ?
jacek
1st October 2006, 17:41
include <QApplication>
#include <QPushButton>
#include <QTextCharFormat>
#include <QTextCursor>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QWidget>
class Test : public QWidget
{
Q_OBJECT
public:
Test() : QWidget( 0 )
{
_textEdit = new QTextEdit();
QPushButton *b = new QPushButton( "Format" );
QVBoxLayout *l = new QVBoxLayout();
l->addWidget( _textEdit );
l->addWidget( b );
setLayout( l );
connect( b, SIGNAL( clicked() ), this, SLOT( format() ) );
}
private slots:
void format()
{
QTextCursor cur( _textEdit->document() );
// indent first paragraph
QTextBlockFormat f( cur.blockFormat() ); // note: you get a copy of the format
f.setIndent( 1 );
cur.setBlockFormat( f ); // here you replace the format used in document
// move to next paragraph
if( cur.movePosition( QTextCursor::NextBlock ) ) {
// select first line
cur.movePosition( QTextCursor::StartOfLine );
cur.movePosition( QTextCursor::EndOfLine, QTextCursor::KeepAnchor );
// note that what "line" means depends on the size of text edit --- resize the
// window to see how it behaves
// underline selected line
QTextCharFormat f( cur.charFormat() );
f.setFontUnderline( true );
cur.setCharFormat( f );
}
}
private:
QTextEdit *_textEdit;
};
int main( int argc, char **argv )
{
QApplication app( argc, argv );
Test t;
t.show();
return app.exec();
}
#include "main.moc"
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.