【问题标题】:QPainter and QTimerQPainter 和 QTimer
【发布时间】:2009-12-09 09:02:17
【问题描述】:

如何使用QPainterQTimer绘制实时正弦曲线

sinus( 2 * w * t + phi )

谢谢。

【问题讨论】:

    标签: qt qpainter


    【解决方案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.
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      相关资源
      最近更新 更多