PDA

View Full Version : QTextBrowser read only mode



Wer_Bn
4th May 2015, 08:48
Hello guys
I am using a QTextBrowser, and its' behaviour it's weird.

When displaying the text, if I select a text, even with read only mode, it deletes the text and replaces with the new one, when using the function
whatever->insertPlainText(const QString&)
If I click with the mouse, it starts writing where I pressed, leaving the rest of the following text after the new text.

Shouldn't it be "read only" mode?
I don't understand why this is happening

Any help would be appreciated. Thanks.

ChrisW67
4th May 2015, 20:44
The text is read-only to the user from the keyboard. The program is able to modify the content at will. The user is able to select text and place the cursor because neither of these modifies the text and both are useful if you are going to have a controlled edting process through the application. In your case, the user selects text and the program replaces that text when you call insertPlainText() because the cursor encompasses the entire selected area (exactly how it would if the user was able to type replacement text).

If you click with the mousewithout selecting a range of text then you have moved the cursor to that location and insertPlainText() inserts the new text at the cursor location. This behaviour is as documented.

If you want to programmtically add text at some other location then regardless of user selections then make sure you position the cursor correctly first. If you want to control the non-modifying interaction allowed to the user then look at QTextEdit::textInteractionFlags().

Wer_Bn
5th May 2015, 15:20
I want the user to be able to select the text (to copy it or something)

So, all I have to do is to place the cursor at the end of the text
I called:

ui->textLogger->textCursor().movePosition(QTextCursor::End);
ui->textLogger->insertPlainText(str);

and still,looks like nothing changes.

What am I doing wrong?

ChrisW67
5th May 2015, 20:36
Define "nothing changes". We cannot see your screen or debugger.

anda_skoa
6th May 2015, 08:38
ui->textLogger->textCursor().movePosition(QTextCursor::End);

So you are changing a temporary (return by value) QTextCursor object. How would that affect textLogger?

Cheers,
_

Wer_Bn
6th May 2015, 10:12
Define "nothing changes". We cannot see your screen or debugger.

Same thing happens. Starts writing in the place I previously clicked, or replaces the selected text.
It doesn't make sense. Because rigth before I write, I change the cursor, so it should just ignore the user input.


So you are changing a temporary (return by value) QTextCursor object. How would that affect textLogger?

Cheers,
_
The function helper states:
"Moves the cursor by performing the given operation n times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false"
So, the way that I called the function, it changes the cursor belonging to the textLogger.

I just found out now, the function returns false, no matter what MoveOperation I chose (QTextCursor::End or QTextCursor::EndOfLine or QTextCursor::EndOfWord)

anda_skoa
6th May 2015, 12:47
So, the way that I called the function, it changes the cursor belonging to the textLogger.

No.

The documentation says "Note that changes on the returned cursor do not affect QTextEdit's cursor"
http://doc.qt.io/qt-5/qtextedit.html#textCursor

You can of course ignore the documentation, but it makes it extremely unlikely that the code you have will do what you need.

Cheers,
_

ChrisW67
7th May 2015, 10:00
This should be closer:


QTextCursor cursor = ui->textLogger->textCursor();
// or
// QTextCursor cursor(ui->textLogger->document());
cursor.movePosition(QTextCursor::End);
cursor.insertText("...");

Wer_Bn
11th May 2015, 21:03
Oh
The textCursor returns a copy... Duhhh
I didn't even realize that was a function...

Thank you very much :D