i am writing a visual basic ide , and i need to add lines numbers to QTextEdit and highlight current line . i have found this tutorial but it is written in java and i write my project in c++ so where to find tutorial like that in c++ , or if there is a ready to use component ?
thanks .
I know that Qt tutorial recommends using
QPlainTextEdit
for text editor implementations, and that the question (except as mentioned in the title), is more general than dealing (absolutely) with aQTextEdit
widget, but I succeeded in implementing the behaviour (line numbers + current line number highlight), and I think this might be helpful for some people (like me) who really want to keep going with theRich Text
widget, and want to share my implementation (which is far from perfect - quite fast coded...).LineNumberArea.h : (Same as "QPlainTextEdit" tutorial)
LineNumberArea.cpp : (Same as "QPlainTextEdit" tutorial)
>> qtextedithighlighter.h :
>> qtextedithighlighter.cpp :
Hope this can help...
Here's the equivalent tutorial in C++:
Qt4: http://doc.qt.io/qt-4.8/qt-widgets-codeeditor-example.html
Qt5: http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
I was looking for a line numbers painting solution for QTextEdit (not QPlainTextEdit), and I found the previous answer with sample code for QTextEdit is useful, but when we set custom line height in QTextEdit's associated SyntaxHighligher, it doesn't work reliably.
To fix that problem, I figured out a simpler way to determine the y coordinate of each block rect by using this code:
And then we can draw line number of each block via:
This seems much simpler and more reliable than calculating the block y coordinate by adding previous block height up.
Hope it helps for someone who is looking for similar solutions.