Results 1 to 3 of 3

Thread: Strange behaviour of painter->drawText

  1. #1
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Strange behaviour of painter->drawText

    Hi,

    I ran into a rather strange problem.
    I have a custom QGraphics Item, that should just print a Text.
    The Paint Function looks like this:
    Qt Code:
    1. void text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QPen temppen=brushes::textpen;
    4. painter->setPen(temppen);
    5. QFont tempfont=fonts::standardfont;
    6. painter->setFont(tempfont);
    7.  
    8. painter->drawText(boundingRect(),Qt::AlignCenter,getUiname());
    9. }
    To copy to clipboard, switch view to plain text mode 

    But whenever this function is called the Text is shifted upwards, and are cut from the Top Border of the BoundingRect.

    If I change the draw method to this:
    Qt Code:
    1. painter->drawText(boundingRect(),Qt::AlignCenter,"Test");
    To copy to clipboard, switch view to plain text mode 

    The text is centered in the Bounding Rectangle as expected.

    getUiname() is rather simple, I don't think that the problem is there.
    Qt Code:
    1. QString uimodule::getUiname() const
    2. {
    3. return uiname;
    4. }
    To copy to clipboard, switch view to plain text mode 

    The only thing left to mention is that getUiname is in the Baseclass of my GraphicsItem. But this should make no difference in my opinion.
    Is this a bug? Any ideas what I could try to fix it?

  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: Strange behaviour of painter->drawText

    The text returned by getUiName() is too wide for the rectangle you are passing to drawText() and is being wrapped to fit the width. Alternatively, the text has a newline char at at the end causing wrapping. The two lines no longer fit the vertical height so drawText() centres them, as requested, and truncates the top and bottom. If you provide drawText() with a pointer to a QRect it will tell you the rectangle you would need to fit the text.

  3. The following user says thank you to ChrisW67 for this useful post:

    0backbone0 (14th October 2015)

  4. #3
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Strange behaviour of painter->drawText

    Yes, the newline-char is causing the trouble.
    When I cut the last Character of the String off, everything works fine.
    Thank you!

Similar Threads

  1. Strange behaviour with QWidget
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2010, 09:32
  2. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 04:03
  3. Subscript text in painter->drawTExt(..)
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 9th January 2008, 07:05
  4. Strange Painter behaviour
    By cwomeijer in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2006, 20:52
  5. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38

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.