You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

wieniew

Beginner

  • "wieniew" is female
  • "wieniew" started this thread

Posts: 1

Location: South Africa

  • Send private message

1

Wednesday, February 13th 2013, 5:59pm

When to subclass QGraphicsScene and which methods to override

Have subclassed QGraphicsScene (GridScene) and QGraphicsView (GridView). In GridScene drawBackGround method draw grid with text lables on the hor and ver axis. Must zoom in, out using QToolButtons, (no scrollbars) on dlg, and pan left, right, up, down with mouse. Cannot get clear view of how to use QGraphicsScene with QGraphicsView.

1. Do override the mouse events in GridView AND GridScene to execute the neccessary actions?

a. If do What do in the GridView and what in the GridScene methods?

b. If not, how do propagate the changes to the QGraphicsScene?

2. When is it necessary to subclass QGraphicsScene, will it be better to draw the grid on the scene in the GridView class?

3. If also override QGraphicsView:: drawBackGround, what needs to be drawn here and what in GridScene:: drawBackGround ?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class GridView : public QGraphicsView
{
    Q_OBJECT
 
public:
    GridView(QWidget* parent = 0);
 
private:
...
public slots:
    void zoomIn ();
    void zoomOut();
 
private:
    GridScene* scene;
    int m_minScaleFactor;
    int m_maxScaleFactor;
    int m_curScaleFactor;
 
};

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
GridView::GridView(QWidget* parent)
    : QGraphicsView(parent)
{
    setGeometry(QRect(0, 0, 474, 621));
    setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
    setDragMode (QGraphicsView::ScrollHandDrag);
    m_minScaleFactor = 0;
    m_maxScaleFactor = 500;
    m_curScaleFactor = 250;
    m_curPanX = 0;
    m_curPanY = 0;
 
    scene = new GridScene;
    this->setScene (scene);
    scene->setSceneRect(QRectF(0, 0, 1500, 1500));
    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);
 
//    setViewportUpdateMode(QGraphicsView::FullViewPortUpdate);
}
 
void GridView::zoomIn ()
{
    qreal scale = qPow(qreal(2), (m_curScaleFactor - 250) / qreal(50));
    QTransform transform;
    transform.scale(scale, scale);
    m_curScaleFactor = transform.m11();
}
 
void GridView::mousePressEvent(QMouseEvent *event)
{
     if (event->button() == Qt::LeftButton)
    {
        m_pan = true;
        m_panStartX = event->x();
        m_panStartY = event->y();
        setCursor(Qt::ClosedHandCursor);
        event->accept();
        return;
    }
    event->ignore();
}
 
void GridView::mouseMoveEvent(QMouseEvent *event)
{
     if (m_pan)
    {
        m_curPanX = (event->x() - m_panStartX);
        m_curPanY = (event->y() - m_panStartY);
        m_panStartX = event->x();
        m_panStartY = event->y();
       event->accept();
        return;
    }
    event->ignore();
}
 
void GridView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_pan = false;
        setCursor(Qt::ArrowCursor);
 
        m_curPanX = event->x();
        m_curPanY = event->y();
        setCursor(Qt::OpenHandCursor);
        event->accept();
        return;
    }
    event->ignore();
}

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <QGraphicsScene>
 
class GridScene : public QGraphicsScene
{
 
public:
    GridScene();
 
protected:
    void drawBackground(QPainter *painter, const QRectF &rect);
 
private:
 ...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
GridScene::GridScene()
{
    minX = 0.0;
    maxX = 10.0;
    numXTicks = 12;
 
    minY = 0.0;
    maxY = 10.0;
    numYTicks = 12;
}
 
void GridScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    QRect rect1(Margin, Margin,
               width() - 2 * Margin, height() - 2 * Margin);
    if (!rect1.isValid())
        return;
 
     for (int i = 0; i <= numXTicks; ++i) {
        int x = rect1.left() + (i * (rect1.width() - 1) / numXTicks);
        double label = minX + (i * spanX() / numXTicks);
        painter->setPen(QPen(QColor(Qt::darkBlue)));
        painter->drawLine(x, rect1.top(), x, rect1.bottom());
        painter->setPen(QPen(QColor(Qt::darkBlue)));
        painter->drawLine(x, rect1.bottom(), x, rect1.bottom() + 5);
        painter->drawText(x - 50, rect1.bottom() + 5, 100, 20,
                          Qt::AlignHCenter | Qt::AlignTop,
                          QString::number(label));
    }
    for (int j = 0; j <= numYTicks; ++j) {
        int y = rect1.bottom() - (j * (rect1.height() - 1)/ numYTicks);
        double label = minY + (j * spanY()
                                          / numYTicks);
        painter->setPen(QPen(QColor(Qt::darkBlue)));
        painter->drawLine(rect1.left(), y, rect1.right(), y);
        painter->setPen(QPen(QColor(Qt::darkBlue)));
        painter->drawLine(rect1.left() - 5, y, rect1.left(), y);
        painter->drawText(rect1.left() - Margin, y - 10, Margin - 5, 20,
                          Qt::AlignRight | Qt::AlignVCenter,
                          QString::number(label));
    }
    painter->drawRect(rect1.adjusted(0, 0, -1, -1));
}