Results 1 to 8 of 8

Thread: readonly QTextEdit with visible Cursor

  1. #1
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default readonly QTextEdit with visible Cursor

    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
    Qt Code:
    1. myTextEdit->setReadOnly(true)
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: readonly QTextEdit with visible Cursor

    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).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: readonly QTextEdit with visible Cursor

    myTextEdit->setEnabled(false);

  4. #4
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readonly QTextEdit with visible Cursor

    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

    Qt Code:
    1. QString text("Hello World");
    2. ui->textEdit->insertPlainText(text);
    3. ui->textEdit->textCursor().movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1);
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: readonly QTextEdit with visible Cursor

    Try the following (I didn't test it):
    Qt Code:
    1. QTextCuresor tc = ui->textEdit->textCursor();
    2. tc.movePosition(QTextCursor::NextCharacter,QTextCursor::KeepAnchor);
    3. ui->textEdit->setCursor(tc);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2011
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: readonly QTextEdit with visible Cursor

    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:

    Qt Code:
    1. myTextEdit.setTextInteractionFlags(myTextEdit.textInteractionFlags() | Qt::TextSelectableByKeyboard);
    To copy to clipboard, switch view to plain text mode 

    By default after you do
    Qt Code:
    1. myTextEdit->setReadOnly(true);
    To copy to clipboard, switch view to plain text mode 
    the text interaction flags are set to simply Qt::TextSelectableByMouse, so you could also just use

    Qt Code:
    1. myTextEdit.setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to changbiyuan for this useful post:

    ArkKup (2nd August 2012)

  8. #7
    Join Date
    Dec 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: readonly QTextEdit with visible Cursor

    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:

    Qt Code:
    1. void MyTextEdit::paintEvent(QPaintEvent* event)
    2. {
    3. QTextEdit::paintEvent(event);
    4. QPainter p(viewport());
    5. p.fillRect(cursorRect(), QBrush(Qt::white));
    6. }
    To copy to clipboard, switch view to plain text mode 

    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!

  9. #8
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    57
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: readonly QTextEdit with visible Cursor

    after calling:

    Qt Code:
    1. myTextEdit -> setReadOnly(true);
    To copy to clipboard, switch view to plain text mode 

    you need to set the flag

    Qt Code:
    1. myTextEdit -> setTextInteractionFlags(myTextEdit -> textInteractionFlags() | Qt::TextSelectableByKeyboard);
    To copy to clipboard, switch view to plain text mode 

    because setReadOnly() doesnt enable that flag

Similar Threads

  1. Replies: 3
    Last Post: 3rd September 2012, 07:32
  2. a Readonly Highlighted TextBlock in QTextEdit?
    By vertusd in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2010, 15:50
  3. Cursor not visible
    By jpujolf in forum Qt Programming
    Replies: 8
    Last Post: 11th May 2007, 14:05
  4. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 09:14
  5. Make the cursor visible in textEdit (Qt3.3.5)
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 24th January 2007, 22: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.