Results 1 to 5 of 5

Thread: Problems with QTextEdit::cursorRect()

  1. #1
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with QTextEdit::cursorRect()

    I am playing with QTextEdit::cursorRect() and having problems. On large fonts, the rect returned to me is wrong, or I am just using this function badly. My code is basically:

    Qt Code:
    1. // p is a QPainter
    2. charWidth = p.fontMetrics().width( charStart );
    3. cursor.setPosition(matchStart, QTextCursor::MoveAnchor);
    4. r = cursorRect( cursor );
    5. p.fillRect( r.x()+charWidth, r.y(), charWidth, r.height(), matchBracesColor );
    To copy to clipboard, switch view to plain text mode 

    and this is not always working (see attached image). I am also attaching a small application which reproduces this problem (the app is tested under linux only, but should work on windows). Read the header of the file for compilation instructions. The code also contains a few font definitions which work or not.

    So... what am I missing here...?
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTextEdit::cursorRect()

    ... no clue...?

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTextEdit::cursorRect()

    My guess is that there is a mixup of coordinate systems... QTextEdit uses viewport coordinates in some places and "normal" widget coordinates in other. If you are not careful enough about which coordinates you get and which you should use for each task (e.g. painting, hit detection, ...) you encounter problems as this one. How do you initialize your painter? Do you invoke any of its coordinate related functions? Do you make use of QAbstractScrollArea viewport margins?
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTextEdit::cursorRect()

    I posted the code since it answers a lot of things:

    1) the painter is initialized to the viewport.
    2) I do not apply any transformations to the painter (is this what you mean?)
    3) I used the viewport margins, but I am not sure it is the cause, since (IMHO) if it was, I would see a constant shift of the position. The shift is relative to the font size on this example.

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTextEdit::cursorRect()

    Proposed fix (not perfect but way better than what you achieved previously) :
    Qt Code:
    1. p.fillRect( r.x() + r.width() / 2, r.y(), charWidth, r.height(), matchBracesColor );
    To copy to clipboard, switch view to plain text mode 

    It falls down to replacing charWidth by r.width() / 2. This is because :
    1. charWidth depends on the "matched" char while cursor is placed between characters bounds...
    2. cursor is in the middle of the rect returned by cursorRect
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Problems installing on MacOS X
    By a5char in forum Installation and Deployment
    Replies: 2
    Last Post: 26th March 2007, 21:45
  2. Replies: 2
    Last Post: 8th March 2007, 22:22
  3. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14
  4. Problems
    By euthymos in forum Installation and Deployment
    Replies: 2
    Last Post: 13th June 2006, 19:11
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

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.