Try this:
#include "matrixscene.h"
#include <QGraphicsRectItem>
MatrixScene
::MatrixScene(QObject *parent
) :{
setSceneRect(0,0,200,200);
}
void MatrixScene::setNumberOfItems(int number)
{
items().clear();
qreal rectWidth = (width() / number) - 5;
qreal rectHeight = (height() / number) - 5;
qreal startX = 1;
qreal startY = 1;
int counterX = 0;
int counterY = 0;
for(counterY = 0; counterY < number; ++counterY) {
startY = counterY * (rectHeight + 5);
for(counterX = 0; counterX < number; ++counterX) {
startX = counterX * (rectWidth + 5);
newItem->setRect(0, 0, rectWidth, rectHeight);
newItem->setPos(startX, startY);
addItem(newItem);
}
}
}
#include "matrixscene.h"
#include <QGraphicsRectItem>
MatrixScene::MatrixScene(QObject *parent) :
QGraphicsScene(parent)
{
setSceneRect(0,0,200,200);
}
void MatrixScene::setNumberOfItems(int number)
{
items().clear();
qreal rectWidth = (width() / number) - 5;
qreal rectHeight = (height() / number) - 5;
qreal startX = 1;
qreal startY = 1;
int counterX = 0;
int counterY = 0;
QGraphicsRectItem *newItem;
for(counterY = 0; counterY < number; ++counterY) {
startY = counterY * (rectHeight + 5);
for(counterX = 0; counterX < number; ++counterX) {
startX = counterX * (rectWidth + 5);
newItem = new QGraphicsRectItem;
newItem->setRect(0, 0, rectWidth, rectHeight);
newItem->setPos(startX, startY);
addItem(newItem);
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks