[英]Function for moving arrays in C++
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 这是.cpp文件中的函数
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. 这是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)
{
}
You don't actually call the function. 您实际上并没有调用该函数。 What you have shown is a function declaration (aka prototype ). 您所显示的是一个函数声明 (也称为prototype )。 All it does is tell the compiler that your function exists and states how it is called. 它所做的只是告诉编译器您的函数存在并说明如何调用它。
Before I go there, you have a few things to iron out first. 在我去那里之前,您需要先解决一些问题。 Namely, the parameters don't make sense. 也就是说,参数没有意义。 Your function creates and lays out icons for your menu. 您的函数创建并布置菜单图标。 So passing in an iconWidget
is confusing. 因此,传递iconWidget
会造成混乱。 Also, you pass arcSize
but then calculate it inside the function. 另外,您传递arcSize
,然后在函数内部进行计算。 I expect that this function should actually be a member of zMenuWidget
. 我希望该函数实际上应该是zMenuWidget
的成员。 Finally, it doesn't just set the locations, it also creates the icons, so the naming is misleading. 最后,它不仅设置位置,还创建图标,因此命名具有误导性。
Let's address all these things in one hit: 让我们一口气解决所有这些问题:
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);
}
}
Notice that I have removed the locally-defined icon
array from this function, because it's defined in your zMenuWidget
class. 注意,我已经从该函数中删除了本地定义的icon
数组,因为它是在您的zMenuWidget
类中定义的。 That's the other hint that you needed to make your function a member of the class. 这是您需要使函数成为类成员的另一个提示。
I also modified the icon creation part to pass the menu widget's pointer to your new icon widget (as its parent). 我还修改了图标创建部分,以将菜单小部件的指针传递给新的图标小部件(作为其父级)。 I assume your iconWidget accepts a parent pointer: 我假设您的iconWidget接受父指针:
icon[i] = new iconWidget(this);
Now in the constructor for zMenuWidget
, you create your icons: 现在在zMenuWidget
的构造函数中,创建图标:
zMenuWidget::zMenuWidget( QWidget *parent)
: QWidget(parent)
{
createAndLayoutIcons(this);
}
That should get you moving in the right direction. 那应该使您朝正确的方向前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.