franco.amato
13th January 2010, 20:46
Hi to all, I can not update the position of a moving triangle in my paintEvent. It stay at the initial position.
This little triangle is used to simulate a time position so it must move indicating time that move when a sound is playing.
Some code:
// here I determine the current time position ( where I would place the triangle
void WaveWidget::setCurrentTimePosition()
{
uint newTimeValue = getCurrentPosInSoundCoord();
/* trasform the sound position in widget coordinate */
qreal samplesPerPixel = m_wave->getSamples() / width();
qreal x_pos = newTimeValue / samplesPerPixel;
if( x_pos == m_CurrentTimePosition )
return; //no change
// I inform that time-position is changed
emit soundPosChanged( m_CurrentTimePosition, x_pos );
/* update current position */
m_CurrentTimePosition = x_pos;
}
The code of the slot that's informed of the time changes is:
void TimeWidget::soundPosChanged( qreal currentTimePosition, qreal newTimePosition )
{
int w = width();
int h = height();
m_CurrentTimePosition = currentTimePosition;
unsigned int rectWidth = ( newTimePosition - m_CurrentTimePosition ) + 4;
//qDebug() << rectWidth;
// determine the rect to update
//m_CurrentTimePosition is the triangle central point
QRect r = QRect( m_CurrentTimePosition - 3, h/2 + 1, rectWidth, h/2 );
// update it
update(r);
// update current position
m_CurrentTimePosition = newTimePosition;
}
Now the code of the paintEvent
void TimeWidget::paintEvent( QPaintEvent* e)
{
int h = height();
int w = width();
QPainter p( this );
p.setRenderHint( QPainter::Antialiasing, true );
p.fillRect( 0, 0, w, h, Qt::lightGray );
p.setBrush(Qt::blue);
static const QPointF points[3] =
{
QPointF( m_CurrentTimePosition -2, h/2),
QPointF( m_CurrentTimePosition + 2, h/2),
QPointF( m_CurrentTimePosition, h)
};
qDebug() << "TimeWidget::paintEvent - centralPoint:" << m_CurrentTimePosition;
p.drawPolygon( points, 3 );
}
I don't know why the triangle doesn't move.
The centralPoint m_CurrentTimePosition is correctly updated as I can see it at shell.
I really don't know where my code is wrong.
I tried to add the following line:
QRect region = e->rect();
as first line in the paintEvent routine and I got the following compiiler error:
error C2027: use of undefined type 'QPaintEvent'
I'm really confused.
Best Regards
This little triangle is used to simulate a time position so it must move indicating time that move when a sound is playing.
Some code:
// here I determine the current time position ( where I would place the triangle
void WaveWidget::setCurrentTimePosition()
{
uint newTimeValue = getCurrentPosInSoundCoord();
/* trasform the sound position in widget coordinate */
qreal samplesPerPixel = m_wave->getSamples() / width();
qreal x_pos = newTimeValue / samplesPerPixel;
if( x_pos == m_CurrentTimePosition )
return; //no change
// I inform that time-position is changed
emit soundPosChanged( m_CurrentTimePosition, x_pos );
/* update current position */
m_CurrentTimePosition = x_pos;
}
The code of the slot that's informed of the time changes is:
void TimeWidget::soundPosChanged( qreal currentTimePosition, qreal newTimePosition )
{
int w = width();
int h = height();
m_CurrentTimePosition = currentTimePosition;
unsigned int rectWidth = ( newTimePosition - m_CurrentTimePosition ) + 4;
//qDebug() << rectWidth;
// determine the rect to update
//m_CurrentTimePosition is the triangle central point
QRect r = QRect( m_CurrentTimePosition - 3, h/2 + 1, rectWidth, h/2 );
// update it
update(r);
// update current position
m_CurrentTimePosition = newTimePosition;
}
Now the code of the paintEvent
void TimeWidget::paintEvent( QPaintEvent* e)
{
int h = height();
int w = width();
QPainter p( this );
p.setRenderHint( QPainter::Antialiasing, true );
p.fillRect( 0, 0, w, h, Qt::lightGray );
p.setBrush(Qt::blue);
static const QPointF points[3] =
{
QPointF( m_CurrentTimePosition -2, h/2),
QPointF( m_CurrentTimePosition + 2, h/2),
QPointF( m_CurrentTimePosition, h)
};
qDebug() << "TimeWidget::paintEvent - centralPoint:" << m_CurrentTimePosition;
p.drawPolygon( points, 3 );
}
I don't know why the triangle doesn't move.
The centralPoint m_CurrentTimePosition is correctly updated as I can see it at shell.
I really don't know where my code is wrong.
I tried to add the following line:
QRect region = e->rect();
as first line in the paintEvent routine and I got the following compiiler error:
error C2027: use of undefined type 'QPaintEvent'
I'm really confused.
Best Regards