我正在制作一个菜单小部件,我需要为每个小部件设置不同的图像。小部件存储在一个数组中。有谁知道我可以用什么来为数组的每个实例设置不同的图像?
如果需要更多信息,请告诉我!
这是小部件的 .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());
}
}