【问题标题】:Creating a QSector Class (using Qt and QPaint)创建一个 QSector 类(使用 Qt 和 QPaint)
【发布时间】:2013-09-06 21:20:47
【问题描述】:

我创建了一个类名 QSector 来绘制如下所示的扇区: http://www.philadelphia-reflections.com/images/GDP_Composition_By_Sector_Graph.jpg

QValue 是一个具有 2 个属性 Label (String) 和 Value (double) 的类 QSector类由4个属性组成,继承自QWidget

  • QPainter(绘制东西)
  • QRect(大小和位置)
  • QVector(存储扇区的所有数据、值和标签)
  • 双倍总计(计算 QValue 值的总和(以稍后计算百分比))

这里是代码::

// <c++>
class QValue
{
public:
    QValue(QString a = "", double b = 0): f_label(a), f_value(b) {}

    double value()  {   return f_value; }
    QString label() {   return f_label; }

    void setValue(double a)  {  f_value = a;    }
    void setLabel(QString a) {  f_label = a;    }

    void set(QString a, double b)   {   f_label = a;    f_value = b;    }

private:
    QString f_label;
    double  f_value;
};

class QSector : public QWidget
{
    Q_OBJECT

public:
    QSector(int width, int height, QWidget *parent = 0)
    : QWidget(parent), f_total(0)
    {
        f_rect = new QRect(1, 1, width - 3 , height - 3);
        this->resize(width, height);
        f_paint = new QPainter;
    }

    void paintEvent(QPaintEvent* event = 0)
    {
        QBrush brush;

        brush.setColor(QColor(25, 25, 255));
        brush.setStyle(Qt::SolidPattern);

        int startAngle  = 0;
        int spanAngle   = 0;

        f_paint->begin(this);

        for (int i = 0; i < f_data.size(); i++)
        {
            int c = ( i * 150) % 255;
            brush.setColor(QColor(c, 25, 255));
            f_paint->setBrush(brush);
    // 5760 = 360 * 16 = 100%;  total = 100% => Value * 5760 / total = span angle
            spanAngle = (5760 * f_data[i].value()) / f_total;

            f_paint->drawPie(*f_rect, startAngle, spanAngle);

            startAngle = spanAngle;
        }

        f_paint->end();
    }

    void add(QString Label, double Value)
    {
        f_data.push_back(QValue(Label, Value));
        f_total = f_total + Value;

        update();   //   => paintEvent();
    }

    void add(QValue a)
    {
        f_data.push_back(a);
        f_total = f_total + a.value();

        update();   //   => paintEvent();
    }

signals:

public slots:

private:
    QPainter *f_paint;
    QRect    *f_rect;

    QVector<QValue>  f_data;
    double  f_total;
};

一切都编译。

问题来了::

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    int w = 300;
    int h = 300;

    QSector test(w, h);

    for (int i = 0, n  = 10; i < n; i++)
        test.add("", 10);

    test.show();

    return app.exec();
}

程序只绘制前 2 个部分并停止(扇区应该有 10 个相等的部分,它只有 2 个)

我不明白为什么它停止绘图。如果我将扇区一分为二,它可以正常工作,但从 3 开始它只绘制 2 个部分

问题总结:http://i.imgur.com/Mssjisd.png (图 1,扇区分为 1) (图 2,扇区分为 2) (图 3,扇区分为 3) (图 4,扇区分为 10)

【问题讨论】:

  • 你不应该在开头使用Q 来命名你的类。它们可能会与 Qt 的未来更改发生冲突,除非您正在构建 Qt in its own namespace

标签: c++ qt graph draw sector


【解决方案1】:

我怀疑

startAngle = spanAngle;

应该是

startAngle += spanAngle;

看起来你只是在同一个角度一遍又一遍地重新绘制同一个饼图。

【讨论】:

  • 谢谢!!我永远不会发现 =,_=
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
相关资源
最近更新 更多