PDA

View Full Version : Confusion with QPoint



therealjag
13th February 2006, 22:32
hello there i have a function called position which is inside another class called shapeItem( ) and it returns a QPoint, but when i do the following it brings up an error saying left hand of operator has no effect. the code is listed below:

1. QPoint mid = shapeItem.position();

where, shapeItem.position is of the following:

2. QPoint ShapeItem::position() const
3. {
4. return myPosition;
5. }

any idea's on how to make QPoint mid obtain the value from the position function??? any input would be much appreciated thanks. Jag

jacek
13th February 2006, 22:50
Could you post the exact error message?

Everall
14th February 2006, 13:26
sounds like you are using shapeItem instead of ShapeItem or vice versa.

Most of the time your code will be more readable if your objects don't have the same name as the class (only different in capitals). You could use Itm or something similar.

Cheers

therealjag
14th February 2006, 15:50
here is the rest of the error message - hope this helps:

sortingbox.cpp: In member function `virtual void SortingBox::paintEvent(QPaintEv
ent*)':
sortingbox.cpp:125: warning: left-hand operand of comma has no effect
sortingbox.cpp:125: error: no matching function for call to `QRect::QRect(QPoint
&, int, QString)'
C:/Qt/4.0.1/include/QtCore/../../src/corelib/tools/qrect.h:37: note: candidates
are: QRect::QRect(const QRect&)
C:/Qt/4.0.1/include/QtCore/../../src/corelib/tools/qrect.h:183: note:
QRect::QRect(int, int, int, int)
C:/Qt/4.0.1/include/QtCore/../../src/corelib/tools/qrect.h:199: note:
QRect::QRect(const QPoint&, const QSize&)
C:/Qt/4.0.1/include/QtCore/../../src/corelib/tools/qrect.h:191: note:
QRect::QRect(const QPoint&, const QPoint&)
C:/Qt/4.0.1/include/QtCore/../../src/corelib/tools/qrect.h:39: note:
QRect::QRect()

jacek
14th February 2006, 15:57
How did you declare that shapeItem variable?

therealjag
14th February 2006, 16:34
basically what i am wanting to do is display a number inside a circle which gets displayed at shapteItem.position(). my code for the displaying of the circes is like below:

QPainter painter(this);

foreach (ShapeItem shapeItem, shapeItems)
{
QPoint text(100,100);
QPoint mid(shapeItem.position())

painter.translate(shapeItem.position());
painter.setBrush(shapeItem.color());
painter.drawPath(shapeItem.path());
painter.translate(-shapeItem.position());
painter.setPen(initialItemColor());
painter.drawText(QRect(mid,text),0,tr("%1").arg(shapeItem.instanceNumber()));

}

ShapeItem is a class in another fle named ShapeItem.cpp and the shapeItem.position was decared in the header file as :

void setPosition(const QPoint &position); and QPoint position() const; where the QPoint is a private variable. my function inside shapeItem is then as follows:

QPoint ShapeItem::position() const
{
return myPosition;
}

and:

void ShapeItem::setPosition(const QPoint &position)
{
myPosition = position;
}

hope that helps, cheers

jacek
14th February 2006, 17:00
All looks OK here.

Is that the first error you get?

therealjag
14th February 2006, 17:14
i got it to work using the following code: QPoint mid(shapeItem.position()); but it doesnt display anything on the screen for some bizarre reason when i try to draw it with this:

painter.setPen(initialItemColor());
painter.drawText(QRect(mid,text),0,tr("%1").arg(shapeItem.instanceNumber()));

i think its a lost hope cos i cant think of aything else to use???!!

jacek
14th February 2006, 18:26
What does this output?
painter.setPen( initialItemColor() );
QRect r(mid,text);
qWarning( "x=%d y=%d w=%d h=%d", r.x(), r.y(), r.width(), r.height() );
painter.drawText( r, QString::number( shapeItem.instanceNumber() ) );

therealjag
14th February 2006, 18:31
i managed to get it working. i was kinda simple in the end, maybe i wasnt explaining my problem properly. all i did was just change the parameters for the drawText function. to the following:

painter.drawText((shapeItem.position( )),tr("%1").arg(shapeItem.instanceNumber()));


thanks for your help - much appreciated. Jag