views:

26

answers:

1

Hi,

how is it possible to add an QPainter arc to the QGraphicsView foreground. I found QGraphicsView.drawForeground (self, QPainter, QRectF), but I don't understand how to use it. I am new to qt. I also know that it is possible to add an art to the QGraphicsScene, but I need the scene for something else. Or is there an easier way to add an arc over the scene to the QGraphicsView? The arc must be variable. Hope someone can help me.

A: 

You will need to create your own subclass of QGraphicsView and implement the drawForeground() method. You can use this code as an example:

MyGraphicsView.h:

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QGraphicsView>

class MyGraphicsView : public QGraphicsView
{
public:
    MyGraphicsView(QWidget * parent = 0);
    MyGraphicsView(QGraphicsScene * scene, QWidget * parent = 0);
    virtual ~MyGraphicsView();

protected:
    void drawForeground(QPainter * painter, const QRectF & rect);
};

#endif  /* MYGRAPHICSVIEW_H */

MyGraphicsView.cpp:

#include "MyGraphicsView.h"

MyGraphicsView::MyGraphicsView(QWidget * parent) :
    QGraphicsView(parent)
{
}

MyGraphicsView::MyGraphicsView(QGraphicsScene * scene, QWidget * parent) :
    QGraphicsView(scene, parent)
{
}

MyGraphicsView::~MyGraphicsView()
{
}

void MyGraphicsView::drawForeground(QPainter * painter, const QRectF & rect)
{
    int startAngle = 30 * 16;
    int spanAngle = 120 * 16;
    painter->drawArc(rect, startAngle, spanAngle);
}
Arnold Spence
Great, I'll try that. Thank you.
acco