0

我正在制作一个菜单小部件,我需要为每个小部件设置不同的图像。小部件存储在一个数组中。有谁知道我可以用什么来为数组的每个实例设置不同的图像?

如果需要更多信息,请告诉我!

这是小部件的 .cpp

#include "iconwidget.h"
#include <QPaintEvent>
#include <QPainter>
#include <QPainterPath>

iconWidget::iconWidget(QWidget *parent) :
    QWidget(parent)
{
    this->resize(ICON_WIDGET_WIDTH,ICON_WIDGET_HEIGHT);
    pressed = false;
}

void iconWidget::paintEvent(QPaintEvent *event)
{
    QRect areatopaint = event->rect();
    QPainter painter(this);
    QBrush brush(Qt::black);

    QPointF center = this->rect().center();

    QPainterPath icon;
    icon.addEllipse(center,30,30);
    painter.drawPath(icon);

    if(pressed)
    {
        brush.setColor(Qt::red);
    }

    painter.fillPath(icon, brush);
}

void iconWidget::mousePressEvent(QMouseEvent *event)
{
    pressed = true;
    update();
    QWidget::mousePressEvent(event);
}

void iconWidget::mouseReleaseEvent(QMouseEvent *event)
{
    pressed = false;
    update();
    QWidget::mouseReleaseEvent(event);
}

这是制作图标并移动它们的功能。我只希望创建的每个图标都有不同的图像。

void zMenuWidget::createAndLayoutIcons(zMenuWidget* const)
{
    int outerRadius = 150;
    int innerRadius = 80;
    int radius = (outerRadius + innerRadius)/2;
    double arcSize = (2.0 * M_PI)/ NUM_ICONS;

    QPointF center;
    center.setX(this->size().width());
    center.setY(this->size().height());
    center /= 2.0;

    //Loop for finding the circles and moving them
    for(int i = 0; i<NUM_ICONS; 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 -= ICON_WIDGET_WIDTH/2.0;
        y -= ICON_WIDGET_HEIGHT/2.0;

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

1 回答 1

0

我假设您的小部件是您已实现的类。如果是这种情况,只需添加一个“图像”成员变量(但您正在实现图像)。然后您的数组存储这些小部件。

在伪代码中:

class Widget
{
    Image image; //however you represent an image. This could be a BYTE pointer.
    //all your other stuff
};

然后,

Widget widget1;
Widget widget2;
widget1.image = something.jpg;
widget2.image = something_else.jpg;

std::vector<Widget> = {widget1,widget2};
于 2013-01-23T23:18:36.903 回答