include <QApplication>
#include <QPushButton>
#include <QTextCharFormat>
#include <QTextCursor>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QWidget>
{
Q_OBJECT
public:
{
l->addWidget( _textEdit );
l->addWidget( b );
setLayout( l );
connect( b, SIGNAL( clicked() ), this, SLOT( format() ) );
}
private slots:
void format()
{
// indent first paragraph
f.setIndent( 1 );
cur.setBlockFormat( f ); // here you replace the format used in document
// move to next paragraph
// select first line
// note that what "line" means depends on the size of text edit --- resize the
// window to see how it behaves
// underline selected line
f.setFontUnderline( true );
cur.setCharFormat( f );
}
}
private:
};
int main( int argc, char **argv )
{
Test t;
t.show();
return app.exec();
}
#include "main.moc"
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"
To copy to clipboard, switch view to plain text mode
Bookmarks