Results 1 to 10 of 10

Thread: Syntax highlight selection problem

  1. #1

    Default Syntax highlight selection problem

    Hey,

    I have a problem with my syntax highlighting when the user selects some text. It seems that when keywords are selected, non keywords on the same line disappear and vice versa.

    It has something to do with the current line marker, that is created in the overridden paint event of QTextEdit.

    Below is an image with no text selected:

    http://nicky.xept.nl/dennis/s1.png

    Now another image with text selected:

    http://nicky.xept.nl/dennis/s2.png

    For a small project file containing just the issue follow this link:

    http://nicky.xept.nl/dennis/highlight.zip

    The code for the paint event

    Qt Code:
    1. void CCodeWidgetText::paintEvent(QPaintEvent *event)
    2. {
    3. QTextEdit::ExtraSelection highlight;
    4. QList<QTextEdit::ExtraSelection> extras;
    5. QList<QTextCursor*> errorCursors;
    6. QTextCursor originalCursor = this->textCursor();
    7.  
    8.  
    9.  
    10. QTextCursor cursor = this->textCursor();
    11. // SAVE THE SELECTION TO LATER RESTORE IT!
    12. // int start = cursor.selectionStart();
    13. // int end = cursor.selectionEnd();
    14.  
    15. // CLEARING THE SELECTION SOLVES THE HIGHLIGHTING PROBLEM
    16. // BUT IS NOT GOOD
    17. // cursor.clearSelection();
    18.  
    19. // Mark current line
    20. highlight.cursor = this->textCursor();
    21. highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
    22. highlight.format.setBackground(QColor(0,255,0));
    23. extras << highlight;
    24.  
    25. // Add extra selections
    26. this->setExtraSelections(extras);
    27.  
    28. QTextEdit::paintEvent(event);
    29.  
    30. // RESTORING THE SELECTION WILL INTRODUCE THE FAULT! (SAVE FIRST ABOVE)
    31. // cursor.setPosition(start, QTextCursor::MoveAnchor);
    32. // cursor.setPosition(end, QTextCursor::KeepAnchor);
    33. // this->setTextCursor(cursor);
    34. }
    To copy to clipboard, switch view to plain text mode 
    Thanks in advance for any help on this,

    Sander
    Last edited by jpn; 1st June 2008 at 11:30. Reason: missing [code] tags

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Syntax highlight selection problem

    is suppose you use qt version < 4.4 your print screen...

    I use qt4.4 if i select only 2 char only this having selection not green line..

    As example you can look:
    http://fop-miniscribus.googlecode.co...lhighlighter.h
    http://fop-miniscribus.googlecode.co...ighlighter.cpp

  3. #3

    Default Re: Syntax highlight selection problem

    I am sorry, but I don't exactly get what you meant..

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Syntax highlight selection problem

    Quote Originally Posted by s.toonen View Post
    I am sorry, but I don't exactly get what you meant..
    Build qt4.4 and test your highlight.pro , and you have a different result.

  5. #5

    Default Re: Syntax highlight selection problem

    Ok thanks for that. However I must use qt 4.3 for this project...
    What is the result exactly?

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Syntax highlight selection problem

    Quote Originally Posted by s.toonen View Post
    Ok thanks for that. However I must use qt 4.3 for this project...
    What is the result exactly?
    exact as
    http://nicky.xept.nl/dennis/s2.png whitout green background , only blue background of selected word...

    Why you not get QRect from Paragraph line nr .. and paint this?
    take the same effect..

    QTextLine return all info..
    http://doc.trolltech.com/4.2/qtextline.html


    if you like real QSyntaxHighlighter subclass this..

    http://wiki.qtcentre.org/index.php?title=XmlHighlighter





    Qt Code:
    1. QTextLine SpanBorder::currentTextLine(const QTextCursor &cursor)
    2. {
    3. const QTextBlock block = cursor.block();
    4. if (!block.isValid())
    5. return QTextLine();
    6.  
    7. const QTextLayout *layout = block.layout();
    8. if (!layout)
    9. return QTextLine();
    10.  
    11. const int relativePos = cursor.position() - block.position();
    12. return layout->lineForTextPosition(relativePos);
    13. }
    14.  
    15. ///// from http://fop-miniscribus.googlecode.com/svn/trunk/GraphicsViewEdit/include/mimedataeditor.cpp
    To copy to clipboard, switch view to plain text mode 


    Imo: on table html nothing selection is possibel only Paragraphs..

  7. #7

    Default Re: Syntax highlight selection problem

    Thanks, I am trying it right now. I integrated your suggested curentTextLine function. In the paint event I now retrieve the QRectF from the currentTextLine. Now I want to draw this, but I have to figure that out now.

  8. #8

    Default Re: Syntax highlight selection problem

    The rects returned to me all have coordinates 0 unfortunately.
    Can you help me how to draw please?

    Btw, the syntax highlighter just has to highlight some keywords, so I don't need a huge class for this project.

  9. #9
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Syntax highlight selection problem

    this to your pro file....

    TEMPLATE = app
    TARGET = xx

    DESTDIR += ./

    CONFIG += console
    CONFIG += qt warn_off debug


    HEADERS += ccodewidgettext.h
    SOURCES += ccodewidgettext.cpp main.cpp
    otherwise you can not see debug output....

    and launch getXcursor() on

    void CCodeWidgetText:aintEvent(QPaintEvent *event)


    ++

    // QT Includes
    #include <QSyntaxHighlighter>
    #include <QTextEdit>
    #include <QWidget>
    #include <QTextDocumentFragment>
    #include <QTextLayout>
    #include <QTextLine>
    #include <QAbstractTextDocumentLayout>


    Qt Code:
    1. void CCodeWidgetText::getXcursor()
    2. {
    3. QTextBlock block = textCursor().block();
    4. int pos = textCursor().position() - block.position();
    5. const QTextLayout *layout = block.layout();
    6. QAbstractTextDocumentLayout *Layout = document()->documentLayout();
    7. QRectF BBrect = Layout->blockBoundingRect ( block );
    8.  
    9. qreal x = 0;
    10. qreal y = 0;
    11.  
    12. QTextLine line = layout->lineForTextPosition(pos);
    13. if (line.isValid() && lastY !=-1 ) {
    14. x = line.cursorToX(pos);
    15. y = line.lineNumber() * line.height();
    16. QPointF Qps = line.position();
    17. qDebug() << "### x " << x;
    18. qDebug() << "### BBrect " << BBrect;
    19. }
    20. else {
    21. x = -1; // delayed init. Makes movePosition() call setX later on again.
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    BBrect has block QRect and x mouse position ..
    so you can paint any area you like ....

    on path /incude from code:
    http://www.qt-apps.org/content/show....?content=80234
    you find more hint to paint text... and swap selection color...

  10. #10

    Default Re: Syntax highlight selection problem

    I got a response from a qt support engineer that I was dealing with a bug in qt 4.3. In qt 4.4 it is fixed but another bug shows up that is scheduled now.

    Thanks for your help.

Similar Threads

  1. QTableView selection problem
    By aiprober in forum Qt Programming
    Replies: 12
    Last Post: 9th June 2008, 11:55
  2. Replies: 1
    Last Post: 1st June 2008, 11:04
  3. Qtreeview selection highlighted problem?
    By thefisher in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2006, 09:50
  4. Selection problem in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2006, 16:02

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.