PDA

View Full Version : about QPainter::drawText()



Nicho
29th November 2013, 03:58
Hi,
The following is my test codes.



char label[MAX_LABEL_LENGTH];
QList<double> lMajorTicks, lMinorTicks;
_pEngine -> buildTicks( _oAttribute._fStart, _oAttribute._fEnd, _nMaxMajorTickNumber, lMajorTicks, lMinorTicks );

int nCount = lMajorTicks.count();
for( int i = 0; i < nCount; i ++ )
{
int nPos = (lMajorTicks[i]-_oAttribute._fStart)*_fScale;
painter -> drawLine( nPos, _oAttribute._nMargin, nPos, _oAttribute._nMargin+_oAttribute._nMajorTickLen );

memset( label, '\0', MAX_LABEL_LENGTH );
sprintf( label, _oAttribute._sMajorLabelFormat, lMajorTicks[i] );
QString text = QString::fromStdString( label );
QRect rc = painter -> boundingRect( QRect(0,0,0,0), Qt::AlignCenter, text );
QRect qrc = QRect( nPos, _oAttribute._nMargin+_oAttribute._nMajorTickLen+15 , rc.width()+10, rc.height()+10 );
if( i > 0 && i < nCount-1);
painter -> drawText( qrc, Qt::AlignCenter, text );

}

If the drawText() is commented off, the program is ok, or it crashes.
I tried many many times but I failed.
I don't know why and how to solve it.
Maybe my way to use QPainter is not right?


Thanks

Nicho

Nicho
29th November 2013, 07:32
Sorry, it's my fault.
The drawLine() before goes out of the range of the device.

Nicho

anda_skoa
30th November 2013, 00:21
Btw, QString::sprintf()

Cheers,
_