Results 1 to 12 of 12

Thread: How to mirror-image text?

  1. #1
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face How to mirror-image text?

    My Qt application is to allow a doctor to display tutorials to patents. It shows graphic images and blocks of text. I am currently displaying the text in a QLabel.

    Now the doctors have a request. Apparently sometimes the patient will be viewing the computer monitor in a mirror. So they asked if there was a way to display the information in mirror image.

    The graphics are easy to transform into mirror images. But so far the text has stumped me. Am I going to have to draw the text as a graphic object in order to transform it into a mirror image, or is there an easier way?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to mirror-image text?

    scale(-1, 1).This is if you use a QGraphicsView.

    Regards

  3. #3
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to mirror-image text?

    Quote Originally Posted by marcel View Post
    scale(-1, 1).This is if you use a QGraphicsView.

    Regards
    Its pretty clear from his post that he is not using graphics view framework.
    I thought of using the scale trick in the paintEvent method but QLabel does some pretty nifty stuff which can't be done while overriding since we don't have access to private members.
    May be we can use a new style to do this and set this as style when mirrored.
    I sometimes feel stylesheet might help too, but i seriously don't have clue here.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to mirror-image text?

    Quote Originally Posted by Gopala Krishna View Post
    Its pretty clear from his post that he is not using graphics view framework.
    OK, I missed that.

    If you subclass QLabel and override it's paint event you can paint the text yourself and map it as you want, since you have access to the label's text. Just adjust the mapping of the painter before painting the text.

    Or the text can be painted separately, in a pixmap, only when the label text is changed. This way you save some time.

    EDIT: actually you can use scale(-1,1) on a QPainter too ( what was I thinking? ).
    If you do it all in the paint event, then you might want to save/restore the painter state between mapping changes.

    Regards

  5. The following user says thank you to marcel for this useful post:

    WinchellChung (30th July 2007)

  6. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to mirror-image text?

    Quote Originally Posted by marcel View Post
    actually you can use scale(-1,1) on a QPainter too ( what was I thinking? ).
    If you do it all in the paint event, then you might want to save/restore the painter state between mapping changes.

    Regards
    Correct Thats what i meant actually. But i had a look at the code of QLabel:aintEvent and it handles bidirectionality, rich text and many more. Reflecting these in the overriden handler is painful.
    But if he just wants the plain text to appear and is ready to sacrifice some special functionality then he can easily go for it.
    But do you think , this can also be done using QStyle ? I have no idea about this
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to mirror-image text?

    Yes, I think it can be done, but it will be more painful than overriding the paint event.

    If he isn't interested in the other functionalities, then he could go ahead and override it.

    Regards

  8. #7
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to mirror-image text?

    Thanks! I'll try over riding the paint event and see how it works.

  9. #8
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: How to mirror-image text?

    So close yet so far.
    I over-rode the paintEvent thusly:

    Qt Code:
    1. void MirrorLabel::paintEvent(QPaintEvent *)
    2. {
    3. QStyle *style = QWidget::style();
    4. QPainter painter(this);
    5. drawFrame(&painter);
    6. int flags = QStyle::visualAlignment(layoutDirection(), QFlag(alignment()));
    7. QRect theFrameRect = frameRect();
    8. painter.save();
    9. painter.scale(-1.0, 1.0);
    10. style->drawItemText(&painter, theFrameRect, flags, palette(), isEnabled(), text(), foregroundRole());
    11. painter.restore();
    12. }
    To copy to clipboard, switch view to plain text mode 
    and alas, the text does not appear. If I change it to painter.scale(1.0, 1.0) the text draws just fine. I have a feeling that the scaling is putting the text outside of the frame rect so it is not drawing.

    I tried setting the style with an explicit Qt::AlignRight, but it had no apparent effect (that is, it right justifies perfectly with a 1,1 scaling, but the text is still invisible with a -1,1 scaling).

    Any ideas as to what I am doing wrong?

  10. #9
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to mirror-image text?

    Yes, scale(-1, 1) would make the y-axis a mirror, and the text would be placed to the left of it. So the solution is to either:

    * translate() half-length to the left, then scale(), then translate() back (the default matrix-manipulation way, easy to understand)
    * scale(), then translate() full length to the right
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  11. The following user says thank you to Michiel for this useful post:

    WinchellChung (31st July 2007)

  12. #10
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to mirror-image text?

    Thanks Michiel! That did the trick.
    The final code looked like this:
    Qt Code:
    1. class MirrorLabel : public QLabel
    2. ....
    3.  
    4. bool isMirrored = true;
    5.  
    6. void MirrorLabel::paintEvent(QPaintEvent *)
    7. {
    8. QStyle *style = QWidget::style();
    9. QPainter painter(this);
    10. drawFrame(&painter);
    11. int flags = QStyle::visualAlignment(layoutDirection(), QFlag(alignment()));
    12. QRect theFrameRect = frameRect();
    13. painter.save();
    14. if (isMirrored) {
    15. painter.scale(-1.0, 1.0);
    16. QPointF translationOffset;
    17. translationOffset.setX(qreal(-theFrameRect.width()));
    18. translationOffset.setY(0.0);
    19. painter.translate(translationOffset);
    20. }
    21. style->drawItemText(&painter, theFrameRect, flags, palette(), isEnabled(), text(), foregroundRole());
    22. painter.restore();
    23. }
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to mirror-image text?

    Small detail: The translate() function is overloaded so you can also use translate(qreal dx, qreal dy). Would save three lines of code. :-)
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  14. #12
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to mirror-image text?

    thanks! Will do.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 15:36
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 16:30
  4. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 10:02
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03

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.