Results 1 to 9 of 9

Thread: QTextBrowser read only mode

  1. #1
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Smile QTextBrowser read only mode

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser read only mode

    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().

  3. #3
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser read only mode

    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?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser read only mode

    Define "nothing changes". We cannot see your screen or debugger.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser read only mode

    Quote Originally Posted by Wer_Bn View Post
    ui->textLogger->textCursor().movePosition(QTextCursor::End);
    So you are changing a temporary (return by value) QTextCursor object. How would that affect textLogger?

    Cheers,
    _

  6. #6
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser read only mode

    Quote Originally Posted by ChrisW67 View Post
    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.

    Quote Originally Posted by anda_skoa View Post
    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)

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser read only mode

    Quote Originally Posted by Wer_Bn View Post
    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,
    _

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser read only mode

    This should be closer:
    Qt Code:
    1. QTextCursor cursor = ui->textLogger->textCursor();
    2. // or
    3. // QTextCursor cursor(ui->textLogger->document());
    4. cursor.movePosition(QTextCursor::End);
    5. cursor.insertText("...");
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser read only mode

    Oh
    The textCursor returns a copy... Duhhh
    I didn't even realize that was a function...

    Thank you very much

Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2014, 10:03
  2. read QTextBrowser line by line
    By navid in forum Newbie
    Replies: 1
    Last Post: 1st March 2010, 15:05
  3. read file in b/w mode
    By eric in forum Qt Programming
    Replies: 3
    Last Post: 11th December 2007, 20:35
  4. How to open a file in Read Write mode
    By merry in forum Qt Programming
    Replies: 13
    Last Post: 16th November 2007, 14:40
  5. read input string --> text mode
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2007, 18:08

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.