0

I am trying to make a function that will move the parts of an array (which is made of widgets) around a circle. It builds and runs but the icons do not appear. Can someone tell me why?

Here is the function in the .cpp file

    void setIconWidgetLocation(iconWidget *w, float arcSize)
{
    int outerRadius = 100;
    int innerRadius = 60;
    int radius = (outerRadius + innerRadius)/2;
    arcSize = 2.0 * M_PI/ 5;

    iconWidget *icon[5];

    QSizeF size = w->size();
    QPointF center(size.width(),size.height());
    center /= 2.0;

    //Loop for finding the circles and moving them
    for(int i = 0; i<5; i++)
    {
        icon[i] = new iconWidget;

        //Finding the Icon center on the circle
        double x = center.x() + radius * sin(arcSize * i);
        double y = center.y() + radius * cos(arcSize * i);

        x -= 10/2;
        y -= 10/2;

        //moves icons into place
        icon[i]->move(x,y);

    }
}

and here is the header file

#ifndef ZMENUWIDGET_H
#define ZMENUWIDGET_H
#include "iconwidget.h"

#include <QWidget>

class zMenuWidget : public QWidget
{
    Q_OBJECT

    iconWidget *icon[5];

public:
    explicit zMenuWidget(QWidget *parent = 0);

    void paintEvent(QPaintEvent *event);

    void resizeEvent(QResizeEvent *event);

signals:

public slots:

};

#endif // ZMENUWIDGET_H

here is the call of the setIconWidgetLocation.

#include "zmenuwidget.h"
#include <QPaintEvent>
#include <QResizeEvent>
#include <QPainter>
#include <QColor>
#include <QPainterPath>
#include <cmath>

setIconWidgetLocation(iconWidget *w, float arcSize);

zMenuWidget::zMenuWidget(QWidget *parent) :
    QWidget(parent)
{

}
4

1 回答 1

1

您实际上并没有调用该函数。您所展示的是一个函数声明(又名原型)。它所做的只是告诉编译器你的函数存在并说明它是如何被调用的。

在我去那里之前,你有几件事要先解决。即,参数没有意义。您的函数为您的菜单创建并布置图标。所以传入 aniconWidget是令人困惑的。此外,您通过arcSize但然后在函数内部计算它。我希望这个函数实际上应该是zMenuWidget. 最后,它不仅设置位置,还创建图标,因此命名具有误导性。

让我们一次性解决所有这些问题:

void zMenuWidget::createAndLayoutIcons()
{
    int outerRadius = 100;
    int innerRadius = 60;
    int radius = (outerRadius + innerRadius)/2;
    double arcSize = 2.0 * M_PI/ 5;

    QSizeF size = w->size();
    QPointF center(size.width(),size.height());
    center /= 2.0;

    //Loop for finding the circles and moving them
    for(int i = 0; i<5; i++)
    {
        icon[i] = new iconWidget(this);

        //Finding the Icon center on the circle
        double x = center.x() + radius * sin(arcSize * i);
        double y = center.y() + radius * cos(arcSize * i);

        x -= 10/2;
        y -= 10/2;

        //moves icons into place
        icon[i]->move(x,y);
    }
}

请注意,我已从icon该函数中删除了本地定义的数组,因为它是在您的zMenuWidget类中定义的。这是您需要使您的函数成为该类的成员的另一个提示。

我还修改了图标创建部分以将菜单小部件的指针传递给您的新图标小部件(作为其父级)。我假设您的 iconWidget 接受父指针:

        icon[i] = new iconWidget(this);

现在在 for 的构造函数中zMenuWidget,创建图标:

zMenuWidget::zMenuWidget( QWidget *parent)
    : QWidget(parent)
{
    createAndLayoutIcons(this);
}

这应该让你朝着正确的方向前进。

于 2013-01-17T04:36:06.530 回答