PDA

View Full Version : Random number of QGraphicsItem



salmanmanekia
8th June 2010, 12:16
Hi All,
I have to draw a custom QGraphicsItem or QGraphicsRectItem ..depending on the user input..for eg if the input is 3...then i should be drawing 9 items in a 3 X 3 matrix...if the input is 4 ..then 16 items in 4 X 4 matrix...the problem is that there can be any value as a input so the cordinates and the size of the item changes...the smaller the item is when the bigger the number is...any solution ??...i was thinking of putting each condition in a if block but obviously that isnt a good idea ..:rolleyes:

high_flyer
8th June 2010, 12:56
What exactly if the question?
Do you need help with the logic of the application, or working with QGraphcis View?

salmanmanekia
8th June 2010, 13:03
i jus need to know if its possible and if it is how it is possible logicaly...

tbscope
8th June 2010, 15:03
Try this:


#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);
}
}
}

salmanmanekia
11th June 2010, 10:20
it compiles succesfuly but there is no output....when i remove the for blocks i can see my view and widgets but as i include the for blocks No Output !:confused:

tbscope
11th June 2010, 12:22
You must call setNumberOfItems somewhere, in your constructor for example.
If you define it as a slot, you can create a combobox or lineedit with button and use it to change the matrix.