QPainter和QTimer

时间:2009-12-09 09:02:17

标签: qt qpainter

如何使用QPainterQTimer来绘制像

这样的实时正弦曲线
sinus( 2 * w * t + phi )

感谢。

1 个答案:

答案 0 :(得分:1)

对于QTimer和绘画之间的互动,我会假设这样的事情:

// 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.
};