Results 1 to 2 of 2

Thread: Highlight the full line (LineUnderCursor)

  1. #1

    Default Highlight the full line (LineUnderCursor)

    We have a selected text in a LineUnderCursor, but we want to hightlight not only the text, we want to hightlight the full line, no text area + text area.

    Do you know any method?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight the full line (LineUnderCursor)

    Yes. QPlainTextEdit::setExtraSelections() (or its equivalent in QTextEdit) does the trick.

    Oh, maybe an example is needed:
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class TextEdit : public QPlainTextEdit {
    5. Q_OBJECT
    6. public:
    7. TextEdit() : QPlainTextEdit() {
    8. connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
    9.  
    10. }
    11. private slots:
    12. void highlightCurrentLine() {
    13. QTextCursor cursor = textCursor();
    14. QTextEdit::ExtraSelection sel;
    15. sel.cursor = cursor;
    16. fmt.setBackground(QColor(220,220,255));
    17. fmt.setProperty(QTextFormat::FullWidthSelection, true); // this is important
    18. sel.format = fmt;
    19. QList<QTextEdit::ExtraSelection> sels;
    20. sels << sel;
    21. setExtraSelections(sels);
    22. }
    23. };
    24.  
    25. #include "main.moc"
    26.  
    27. int main(int argc, char **argv) {
    28. QApplication app(argc, argv);
    29. TextEdit te;
    30. te.insertPlainText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi non ipsum eu risus viverra vestibulum id eget magna. Sed ultricies ultricies odio ac tincidunt. Phasellus volutpat quam non nisi aliquet vehicula. Vivamus placerat ligula lacus, in aliquet metus. Fusce vulputate mauris non turpis mollis ut fermentum eros laoreet. Quisque a neque metus, eleifend venenatis ipsum. Duis ullamcorper, est eget pharetra auctor, mauris mauris placerat purus, sit amet placerat quam lorem aliquam leo. Aliquam erat volutpat.\n"
    31. "Mauris aliquam pulvinar mattis. Proin ac lorem nec ligula lacinia rhoncus. Maecenas mattis urna non augue pellentesque posuere. Pellentesque luctus, purus in vehicula auctor, neque nulla auctor felis, a egestas purus metus at mi. Fusce odio tortor, laoreet vitae hendrerit in, condimentum ut massa. Fusce viverra volutpat lectus ut rhoncus. Ut molestie suscipit urna, quis tristique ante auctor sed. Donec dignissim, risus aliquet condimentum lobortis, orci ante pharetra dolor, vitae mollis ligula massa in elit. Nam risus orci, sollicitudin vel placerat ut, porta sit amet dui.");
    32. te.show();
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 5th October 2012 at 00:34.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 7th November 2012, 16:59
  2. QtXml parse error at line 1 column line 1
    By vinod sharma in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th September 2012, 05:02
  3. client sending data line by line to server in Qt
    By ajay in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2012, 00:06
  4. QPlainTextEdit highlight selected line
    By ArkKup in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2011, 09:45
  5. Replies: 3
    Last Post: 13th August 2010, 11:50

Tags for this Thread

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.