【问题标题】:How to add an arc to the foreground of a QGraphicsView如何在 QGraphicsView 的前景中添加弧线
【发布时间】:2010-09-15 12:44:56
【问题描述】:

如何将 QPainter 弧添加到 QGraphicsView 前景。 我找到了 QGraphicsView.drawForeground (self, QPainter, QRectF),但我不明白如何使用它。我是qt的新手。我也知道可以向 QGraphicsScene 添加艺术,但我需要场景来做其他事情。或者有没有更简单的方法将场景中的弧线添加到 QGraphicsView?弧必须是可变的。 希望可以有人帮帮我。

【问题讨论】:

    标签: qt qt4 qgraphicsview


    【解决方案1】:

    您需要创建自己的QGraphicsView 子类并实现drawForeground() 方法。您可以使用此代码作为示例:

    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);
    }
    

    【讨论】:

    • 太好了,我会试试的。谢谢。
    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多