Results 1 to 6 of 6

Thread: QGraphicsTextItem ensure cursor visible

  1. #1
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsTextItem ensure cursor visible

    Hi,

    I have a very simple text editor based on QGraphicsTextItem. I would like to ensure that the blinking cursor is visible if I move the cursor. How can I get the actual cursor position? (The coordinates, not the character number)
    I have implemented scroll bar in my editor so I can easily scroll to any part of the text, just I need to know the cursor's actual coordinate in the document. Is it possible?

    (I cannot use QTextEdit in a proxy widget, but I think, what I need is a similar function to QTextEdit's ensurecursorvisible)

  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: QGraphicsTextItem ensure cursor visible

    (I cannot use QTextEdit in a proxy widget,
    Why not?

    just I need to know the cursor's actual coordinate in the document.
    Wont this do?:
    Qt Code:
    1. myGraphicsTextItem->textCursor().position();
    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.

  3. #3
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsTextItem ensure cursor visible

    Why not?
    Because of performance problems and the boss don't want it

    myGraphicsTextItem->textCursor().position();
    No, it just gives me the character number in the text. (e.g.: the cursor is at the 149th character in the document, but I don't know the coordinates of it)

  4. #4
    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: QGraphicsTextItem ensure cursor visible

    Without using QTextEdit, I think you will have to go low level for that.
    ==========================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.

  5. #5
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsTextItem ensure cursor visible

    At last I found the way:

    Qt Code:
    1. QGraphicsTextItem::keyPressEvent(event);
    2. QTextBlock block = document()->findBlock(textCursor().position());
    3. QTextLine line = block.layout()->lineForTextPosition(textCursor().positionInBlock());
    4. qreal cursor_y_position = document()->documentLayout()->blockBoundingRect(block).y() + line.y();
    5. qreal line_height = line.height();
    6. qDebug("New cursor position: %.2f %.2f", cursor_y_position, line_height);
    7. if ( _actual_position > cursor_y_position )
    8. {
    9. ScrollToPosition(cursor_y_position);
    10. }
    11. if ((_actual_position + _height) < (cursor_y_position + line_height))
    12. {
    13. ScrollToPosition(cursor_y_position + line_height - _height);
    14. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2009
    Location
    Sacramento, CA
    Posts
    24
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsTextItem ensure cursor visible

    Nice Code snippet, but what are actual_position and _height?

    Does actual_position = textCursor().position() and _height = document().size().height()?

Similar Threads

  1. How to ensure widget is visible
    By arturo182 in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2009, 00:45
  2. How can to change the QGraphicsTextItem 's cursor width?
    By robertkun in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2009, 03:49
  3. cursor in qgraphicstextitem
    By Noxxik in forum Qt Programming
    Replies: 0
    Last Post: 15th March 2009, 12:04
  4. Hiding the cursor in QGraphicsTextItem
    By yartiss in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2008, 13:19
  5. Cursor not visible
    By jpujolf in forum Qt Programming
    Replies: 8
    Last Post: 11th May 2007, 14:05

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.