PDA

View Full Version : QGraphicsView wont show every item



abbapatris
4th September 2007, 18:21
I created a custom QGraphicsItem, subclassed it and everything. I run that with my program and it starts to hide my QGraphicsLineItems and my QGraphicsElipseItem. I am not sure why. I have narrowed it down to my customItem if anyone has any clue could you late me know. The code is below.

#include "SearchArea.h"

SearchArea::SearchArea(double rangeExtent, double centerRange,
double centerAzimuth, double azimuthExtent,
QGraphicsScene *parent)
: QGraphicsItem(0,parent)
{
radarCenterRange = rangeExtent;
radarRangeExtent = centerRange;
radarCenterAzimuth = centerAzimuth;
radarAzimuthExtent = azimuthExtent;
zoomValue = 1;
cPoint = new QPoint();
cPoint->setX(0);
cPoint->setY(0);
gPen = new QPen();
gPen->setColor(Qt::green);

}

SearchArea::~SearchArea()
{

}
////////////////////////////////////////////////////////////////////////
void SearchArea::drawFence()
{
double rangeExtentZoom = radarRangeExtent/10 * zoomValue;
double centerRangeZoom = radarCenterRange/10 * zoomValue;
double rangeExtentZoomFarthest = centerRangeZoom + rangeExtentZoom/2;
double rangeExtentZoomClosest = centerRangeZoom - rangeExtentZoom/2;
double azimuthExtentLowest = radarCenterAzimuth - radarAzimuthExtent/2;
double azimuthExtentHightest = radarCenterAzimuth + radarAzimuthExtent/2;
QPoint startLowerLine, finishLowerLine, startUpperLine, finishUpperLine;
gPainter->setPen(gPen->color());


startLowerLine = GetArkPoint(radarCenterAzimuth, azimuthExtentLowest, rangeExtentZoomClosest);
finishLowerLine = GetArkPoint(radarCenterAzimuth, azimuthExtentLowest, rangeExtentZoomFarthest);
startUpperLine = GetArkPoint(radarCenterAzimuth, azimuthExtentHightest, rangeExtentZoomClosest);
finishUpperLine = GetArkPoint(radarCenterAzimuth, azimuthExtentHightest, rangeExtentZoomFarthest);

double radius1 = sqrt(pow(finishLowerLine.x(),2)+pow(finishLowerLin e.y(),2));
double height1 = radius1*2;

gPainter->drawLine(startUpperLine, finishUpperLine);
gPainter->drawLine(startLowerLine, finishLowerLine);

drawArcFence(startUpperLine,azimuthExtentLowest,ra darAzimuthExtent);
drawArcFence(finishUpperLine,azimuthExtentLowest,r adarAzimuthExtent);


gPainter->end();

}
////////////////////////////////////////////////////////////////////////
void SearchArea::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
gPainter = painter;
Q_UNUSED(option);
Q_UNUSED(widget);

drawFence();

}
////////////////////////////////////////////////////////////////////////
QPoint SearchArea::GetArkPoint(double centerAzimuth, double azimuth, double rangeExtent)
{
QPoint lowerArkPoint;
lowerArkPoint.setY( (sin (azimuth*PI/180) * rangeExtent )*-1);
lowerArkPoint.setX( (cos (azimuth*PI/180) * rangeExtent) );
lowerArkPoint.setX( lowerArkPoint.x());
lowerArkPoint.setY( lowerArkPoint.y());

return lowerArkPoint;

}

////////////////////////////////////////////////////////////////////////
void SearchArea::drawArcFence(QPoint point, double azimuth, double azimuthExtent)
{
gPainter->setPen(gPen->color());
point.setX(point.x());
point.setY(point.y());
double radius = sqrt(pow(point.x(),2)+pow(point.y(),2));
double height = radius*2;
gPainter->drawArc(0-radius,0-radius,height,height,azimuth*16,azimuthExtent*16);
}
////////////////////////////////////////////////////////////////////////
QRectF SearchArea::boundingRect() const
{
return QRectF(0, 0,0, 0);
}
////////////////////////////////////////////////////////////////////////
void SearchArea::setPen(QPen * pen)
{
gPen = pen;
}
void SearchArea::setLocation(QPoint *centerPoint)
{
cPoint = centerPoint;
}

marcel
4th September 2007, 18:40
OK.
The first error I see is that you return an empty bounding rect.
The bounding rect of a QGraphicsItem is used by the view to know what/where to render.

This problem does not seem to be the logical reason for the problem in your post, but it is still an error.

Could you correct that and post back? Or at least say why do you return an empty bounding rect.


Other than that, your graphics item might cover everything else. This could be because of all the computations you do. Maybe some of them are wrong and you get very big values.

But first try to correct the bounding rect.

Regards

abbapatris
4th September 2007, 18:49
The bounding issue is due to me playing around with it to get it to work. I forgot to change it back to its original size. But it was at the correct size and still gave the same results.

marcel
4th September 2007, 18:57
OK. Then could you paste a set of values that you pass to the item's constructor?


Regards

abbapatris
4th September 2007, 19:05
SearchArea * radarItem = new SearchArea(600, 1000, 70, 120, this);

"this" is the scene that I am using.

marcel
4th September 2007, 19:25
The values seem OK. At least the ones I've computed.
They biggest dimension is not bigger than ~300.

So, what if you draw a border around your item? Draw a rect exactly on the bounding box. This way you will see clearly how much it expands.

BTW, what's with that QGraphicsItem constructor with two parameters?

Regards

abbapatris
5th September 2007, 01:26
the two variables in the constructor were copied from someone else's code. I am just learning so I tried what they put.

abbapatris
5th September 2007, 11:47
I got all the objects to show up correctly. I just removed the line that says gPainter->end(). But I still have a problem refreshing after scrolling the graphics view so the item is no longer on the screen.