PDA

View Full Version : readonly QTextEdit with visible Cursor



kerim
23rd March 2011, 11:50
hello all,

i have the following problem hope one can help:
i am writing a tool wich displays a textfile using QTextEdit.
i want that QTextEdit to be readonly, since text shall not be modified by the user
but just inspected.

after having set
myTextEdit->setReadOnly(true) the cursor gets invisible when trying to click somewhere into the text.

i am thinking to implement a current-line functionality and display at what line the cursor is currently placed and need that cursor to be visible during movement (scrolling).

how would one do that!?
thnx alot.
cheers.

high_flyer
23rd March 2011, 12:01
Hmm... this is the normal behavior for non editable text edits...
I think the easiest way would be to always make a selection of one character at the cursor position, this will mark that character making the cursor position visible (but not the cursor it self, but for the purpose you need it I think it should do fine).

Gokulnathvc
23rd March 2011, 12:39
myTextEdit->setEnabled(false);

kerim
23rd March 2011, 12:58
hmmm, ok.
i assume there is no other class than QTextEdit that would provide the mentioned behaviour?

how would the code fragment look like to mark a single character at current cursor postion (means: the character after the cursor) since QTextCursor only provides to select words lines or blocks (QTextCursor::select(..))
having read the doc for QTextCursor::movePosition(..) i assumed the following would work right after having inserted text to the QTextEdit but it doesnt :(



QString text("Hello World");
ui->textEdit->insertPlainText(text);
ui->textEdit->textCursor().movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1);



myTextEdit->setEnabled(false);


that just disables the QTextEdit widget (gets gray) .. but still no cursor .. after having put text into the qtextedit one cant even scroll it, so its even worse ;)

high_flyer
23rd March 2011, 14:12
Try the following (I didn't test it):


QTextCuresor tc = ui->textEdit->textCursor();
tc.movePosition(QTextCursor::NextCharacter,QTextCu rsor::KeepAnchor);
ui->textEdit->setCursor(tc);

changbiyuan
27th April 2011, 22:19
I wanted to do the same thing and couldn't find a straight answer about it anywhere on the web. But something led me to try setTextInteractionFlags() and I had some luck with that, so I'll post this here in case it's useful to anyone else. It seems you can get a readonly QTextEdit with a visible cursor (albeit one that doesn't blink, at least on Windows) by doing this:


myTextEdit.setTextInteractionFlags(myTextEdit.text InteractionFlags() | Qt::TextSelectableByKeyboard);

By default after you do
myTextEdit->setReadOnly(true); the text interaction flags are set to simply Qt::TextSelectableByMouse, so you could also just use


myTextEdit.setTextInteractionFlags(Qt::TextSelecta bleByMouse | Qt::TextSelectableByKeyboard);

kralyk
4th September 2013, 21:08
I find all of the above solutions quirky.

However, I found out it's farily easy to draw the cursor yourself. All you need to do is subclass the QTextEdit, set it readonly and reimplement the paintEvent like this:



void MyTextEdit::paintEvent(QPaintEvent* event)
{
QTextEdit::paintEvent(event);
QPainter p(viewport());
p.fillRect(cursorRect(), QBrush(Qt::white));
}


In this example I simply draw a white filled rectangle in place of the cursor (it creats a block cursor), but you can draw pretty much anything in there: An I-Bream, a font glyph, anything. You can make it blink or not blink. This way the text cursor becomes completely customizable.

Cheers!

cic
2nd January 2014, 11:19
after calling:




myTextEdit -> setReadOnly(true);



you need to set the flag




myTextEdit -> setTextInteractionFlags(myTextEdit -> textInteractionFlags() | Qt::TextSelectableByKeyboard);



because setReadOnly() doesnt enable that flag