[Qt-interest] Fixed text on a graphics view

Murphy, Sean M. sean.murphy at gd-ais.com
Wed Sep 16 20:42:35 CEST 2009


> I need to display a fixe text which I need to update each time the
mousse move on 
> the view. I need this text to be on a corner of the view, being all
the time
> visible whenever I resize the view or scroll in the view. and finaly I
need this
> text to be unchange when I change the scale of my view.
>
> I've create a QGraphicsItem with the flag
QGraphicsItem.ItemIgnoresTransformations
> set to True.

First, don't use a QQGraphicsItem.  Graphics items really want to stay
in one place relative to scene locations.  The
"ItemIgnoresTransformations" flag only ignores the scale of the item as
the zoom of the QGraphicsView changes, but the item will still be at the
same place on the scene that it was before, so the result doesn't leave
you with text in the corner of your view where you want it.
>
> On my view, I update this item in the mousseMoveEvent but how can I
manage to have
> this item in a corner whenever I change my view?

What you need to do is create your own class that inherits from
QGraphicsView.  You'll then use a QPainter to paint your desired text on
the *viewport* widget of your view class.  Let's call your class
"txtOverlayView".

You need to re-implement the mouseMoveEvent() function to force a
repaint on every mouse movement, and the paintEvent() function to paint
your text.

So your header should look like this:

#ifndef TXTOVERLAYVIEW_H
#define TXTOVERLAYVIEW_H

#include <QGraphicsView>

class txtOverlayView : public QGraphicsView
{
    Q_OBJECT
public:
    txtOverlayView(QWidget* parent=0);
    txtOverlayView(QGraphicsScene* scene, QWidget* parent=0);

protected:
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);

private:
    QPoint mousePos;

};

#endif // TXTOVERLAYVIEW_H

And your .cpp file should look like:

#include "txtoverlayview.h"
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>

txtOverlayView::txtOverlayView(QWidget* parent):QGraphicsView(parent)
{
    setMouseTracking(true);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

txtOverlayView::txtOverlayView(QGraphicsScene* scene, QWidget*
parent):QGraphicsView(scene,parent)
{
    setMouseTracking(true);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

void txtOverlayView::mouseMoveEvent(QMouseEvent *event)
{
    mousePos = event->pos();
    viewport()->update();
}

void txtOverlayView::paintEvent(QPaintEvent *event)
{
    QGraphicsView::paintEvent(event); // paint contents normally

    // draw text over the top of the viewport
    QPainter p(viewport());
    QPoint pt(0,10); // location for text string, in this case upper
left corner
    QString str;
    // set string text, in this case the mouse position value
    str = QString("(") + QString::number(mousePos.x()) + QString(", ") +

          QString::number(mousePos.y()) + QString(")");
    p.drawText(pt, str);
    p.end();
}




More information about the Qt-interest-old mailing list