tags:

views:

80

answers:

1

Hi

How to use QPainter and QTimer to draw a real time sinusoid like sinus(2*w*t+ phi).

thanks.

A: 

I would assume something like this, for the interaction between QTimer and painting:

// Periodically paints a sinusoid on itself.
class SinPainter : public QWidget 
{
    Q_OBJECT
public:
    SinPainter( QWidget *parent_p = NULL ) : 
        QWidget( parent_p ), 
        m_timer_p( new QTimer( this ) ),
        m_t( 0.0 )
    {
        // When the timer goes off, run our function to change the t value.
        connect( m_timer_p, SIGNAL( timeout() ), SLOT( ChangeT() ) );
        // Start the timer to go off every TIMER_INTERVAL milliseconds
        m_timer_p->start( TIMER_INTERVAL );
    }

    // ...

protected slots:
    void ChangeT()
    {
        // Change m_t to the new value.
        m_t += T_INCREMENT;
        // Calling update schedules a repaint event, assuming one hasn't 
        // already been scheduled.
        update();
    }

protected:
    void paintEvent( QPaintEvent *e_p )
    {
        QPainter painter( this );
        // Use painter and m_t to draw your current sinusoid according 
        // to your function.
    }

private:
    QTimer *m_timer_p;
    double m_t; // <-- Or whatever variable type it needs to be.
};
Caleb Huitt - cjhuitt
Very Well, Thank You.
Hannibal