Hey,
I appreciate your effort to prepare the example, in return (and because I had some spare time) I've got solution for you
It seems in fact to be a bug in Qt, from what I've found the paint event for QPlainTextEdit contains wrong rectangle.
You can fix it yourself without affecting Qt source, here's how:
class FixedQPlainTextEdit : public QPlainTextEdit // I guess the name tells you everything :)
{
public:
FixedQPlainTextEdit
( QWidget* parent
= 0 ) : QPlainTextEdit
( parent
) {}
protected:
{
QPaintEvent* fixed_event
= new QPaintEvent( e
->rect
().
adjusted( -1,
-1,
0,
0 ) );
// left and top edge need to be adjusted by -1 QPlainTextEdit::paintEvent( fixed_event );
delete fixed_event;
}
};
// below is your example code using the FixedQPlainTextEdit class
MainWindow
::MainWindow( QWidget* p
) :
{
QPlainTextEdit* e = new FixedQPlainTextEdit();
e->appendPlainText( "#include <QtGui/QApplication>" );
e->appendPlainText( "#include \"mainwindow.h\"" );
e->appendPlainText( "" );
e->appendPlainText( "int main(int argc, char *argv[])" );
e->appendPlainText( "{" );
e->appendPlainText( " QApplication a(argc, argv);" );
e->appendPlainText( " MainWindow w;" );
e->appendPlainText( " w.show();" );
e->appendPlainText( "" );
e->appendPlainText( " return a.exec();" );
e->appendPlainText( "}" );
this->setCentralWidget( e );
QList<QTextEdit::ExtraSelection> extraSelections;
QBrush backBrush
( Qt
::yellow );
QBrush textBrush
( Qt
::black );
QPen outlinePen
( Qt
::gray,
1 );
QString highlighText
= "QApplication";
for( int i=0; i<e->document()->blockCount(); i++ )
{
QTextBlock block
= e
->document
()->findBlockByNumber
( i
);
if( block.isValid() )
{
int p;
if( (p=text.indexOf(highlighText)) != -1 )
{
int pos = block.position() + p;
selection.cursor.setPosition( pos );
selection.
cursor.
setPosition( pos
+highlighText.
length(),
QTextCursor::KeepAnchor );
selection.format.setBackground( backBrush );
selection.format.setForeground( textBrush );
selection.
format.
setProperty( QTextFormat::OutlinePen, outlinePen
);
extraSelections.append( selection );
}
}
}
e->setExtraSelections( extraSelections );
}
class FixedQPlainTextEdit : public QPlainTextEdit // I guess the name tells you everything :)
{
public:
FixedQPlainTextEdit( QWidget* parent = 0 ) : QPlainTextEdit( parent ) {}
protected:
void paintEvent( QPaintEvent* e )
{
QPaintEvent* fixed_event = new QPaintEvent( e->rect().adjusted( -1, -1, 0, 0 ) ); // left and top edge need to be adjusted by -1
QPlainTextEdit::paintEvent( fixed_event );
delete fixed_event;
}
};
// below is your example code using the FixedQPlainTextEdit class
MainWindow::MainWindow( QWidget* p )
:
QMainWindow( p )
{
QPlainTextEdit* e = new FixedQPlainTextEdit();
e->appendPlainText( "#include <QtGui/QApplication>" );
e->appendPlainText( "#include \"mainwindow.h\"" );
e->appendPlainText( "" );
e->appendPlainText( "int main(int argc, char *argv[])" );
e->appendPlainText( "{" );
e->appendPlainText( " QApplication a(argc, argv);" );
e->appendPlainText( " MainWindow w;" );
e->appendPlainText( " w.show();" );
e->appendPlainText( "" );
e->appendPlainText( " return a.exec();" );
e->appendPlainText( "}" );
this->setCentralWidget( e );
QList<QTextEdit::ExtraSelection> extraSelections;
QBrush backBrush( Qt::yellow );
QBrush textBrush( Qt::black );
QPen outlinePen( Qt::gray, 1 );
QString highlighText = "QApplication";
for( int i=0; i<e->document()->blockCount(); i++ )
{
QTextBlock block = e->document()->findBlockByNumber( i );
if( block.isValid() )
{
QString text = block.text();
int p;
if( (p=text.indexOf(highlighText)) != -1 )
{
int pos = block.position() + p;
QTextEdit::ExtraSelection selection;
selection.cursor = QTextCursor(e->document());
selection.cursor.setPosition( pos );
selection.cursor.setPosition( pos+highlighText.length(), QTextCursor::KeepAnchor );
selection.format.setBackground( backBrush );
selection.format.setForeground( textBrush );
selection.format.setProperty( QTextFormat::OutlinePen, outlinePen );
extraSelections.append( selection );
}
}
}
e->setExtraSelections( extraSelections );
}
To copy to clipboard, switch view to plain text mode
Edit:
Btw the issue was also visible if you placed the cursor under the highlighted field.
Bookmarks