Results 1 to 4 of 4

Thread: QGraphicsView "un"scale text

  1. #1
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default QGraphicsView "un"scale text

    Hi,

    I have a graphicsview that gets scaled. I want a line + text that always stays in the top left corner whatever the scaling or scrollbar position is.

    I managed (with the help of some previous threads) to draw a line but the font is always scaled. Setting a pixelsize depending on scaling factor did not work. If I rescale the painter (e.g. resetTransform()) I get always the same fontsize but then I lose the point's position where to draw the text ...

    Qt Code:
    1. void GraphicsView::drawForeground(QPainter *painter, const QRectF &rect)
    2. {
    3.  
    4. QRectF scenerect = QRectF(mapToScene(0,0), mapToScene(width(), height()));
    5.  
    6. QPointF p1(scenerect.topLeft().x() + (10/matrix().m11()), scenerect.topLeft().y() + (10/matrix().m22()));
    7. QPointF p2(p1.x() + (50/matrix().m11()), p1.y() + (10/matrix().m22()));
    8. QRectF r(p1, p2);
    9.  
    10. painter->drawLine(r.topLeft(), r.topRight()); //ok
    11. painter->drawText(r, QString::number(10)); //?
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView "un"scale text

    drawForeground works in terms of scene coordinates and you want viewport coordinates. It would be easiest to apply an event filter on the view's viewport() and override its paintEvent using the event filter or put your own widget as the viewport and change its paintEvent so that you both call the base class implementation and draw something on it yourself. You can also position a QLabel object in the view (but not in the scene).

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

    janus (2nd February 2009)

  4. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QGraphicsView "un"scale text

    thx, i added a lable and installed the eventfilter on the it.

    Qt Code:
    1. label = new QLabel(ui.graphicsView->viewport());
    2. label->setText("text");
    3. label->resize(50, 15);
    4. label->installEventFilter(this);
    5.  
    6. }
    7.  
    8. bool ViewWidget::eventFilter(QObject *obj, QEvent *event)
    9. {
    10.  
    11. qDebug() << obj << event;
    12. if (obj == label) {
    13. if (event->type() == QEvent::Paint) {
    14. QPainter painter(label);
    15. painter.drawLine(QPointF(label->rect().bottomLeft().x() + 5, label->rect().bottomLeft().y()), label->rect().bottomRight());
    16. painter.drawText(QPointF(label->rect().bottomLeft().x() + 5, label->rect().bottomLeft().y() -2), "test");
    17.  
    18. return true;
    19.  
    20. }
    21. }
    22. else
    23. return QWidget::eventFilter(obj, event);
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView "un"scale text

    If you add a label then you can simply call its functions instead of using an event filter. If you want the event filter, there is no point is using QLabel - use QWidget instead.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. Replies: 9
    Last Post: 22nd February 2008, 16:22
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.