PDA

View Full Version : Problem of highlighting text in QPlainTextEdit



topfortune
17th April 2012, 05:48
Hello,

My Qt SDK version 4.7.4

I use QPlainTextEdit::setExtraSelections() to highlight some words in QPlainTextEdit. Following is my code:


int pos = block.position() + p;

QTextEdit::ExtraSelection selection;
selection.cursor = QTextCursor(ui->plainTextEdit->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 );

Everything seems Ok. But when I move the caret on the position of highlighted text. The highlighted text is not diaplay ok. Please refer to the screenshot.

7594

Added after 4 minutes:

I attached a demo for the problem.

Can anybody help me?

dennis81
17th April 2012, 09:52
maybe you can force periodical the paint event. It looks like Qt <-> WinXP problem. I have got a similar problem with QPlaintextEdits on some Windows machines with Qt 4.5.3.

topfortune
17th April 2012, 11:08
maybe you can force periodical the paint event. It looks like Qt <-> WinXP problem. I have got a similar problem with QPlaintextEdits on some Windows machines with Qt 4.5.3.

I built and run the demo on Ubuntu. And the problem also exists on Linux. I think it must be a bug of Qt. I really don't want to modify the source code of Qt for the problem.

topfortune
18th April 2012, 05:35
Can anybody help me? Please....

Spitfire
18th April 2012, 18:07
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:
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 );
}


Edit:
Btw the issue was also visible if you placed the cursor under the highlighted field.

topfortune
19th April 2012, 03:51
Hi Spitfire,

It's perfect! Thank you so much for your solution. Thank you so much!!!