qtqwtoscilloscope

how does qwt oscilloscope example's samplingthread class work in the project?


I am having a difficulty in understanding qwt oscilloscope example. i understand most of the program roughly, but i can not find the linkage between samplingthread class and plot class.

It seems like chart samples are from samplingthread and it is provided to QwtPlotCurve object in plot class.

However i can not find linkage between samplingthread object and plot object. But when i change the frequency value in samplingthread object, it is applied and appears on plot object (canvas).

Below is part of code (from main.cpp) i do not really understood but please reference full project (need decompress i think) by downloading from http://sourceforge.net/projects/qwt/files/qwt/6.1.2/.

int main( int argc, char **argv )
{
QApplication app( argc, argv );
app.setPalette( Qt::darkGray );

MainWindow window;
window.resize( 800, 400 );

SamplingThread samplingThread;
samplingThread.setFrequency( window.frequency() ); // window.frequency()'s type is double 
samplingThread.setAmplitude( window.amplitude() ); // window.amplitude()'s type is double
samplingThread.setInterval( window.signalInterval() ); // window.signalInterval()'s type is double

window.connect( &window, SIGNAL( frequencyChanged( double ) ),
    &samplingThread, SLOT( setFrequency( double ) ) );
window.connect( &window, SIGNAL( amplitudeChanged( double ) ),
    &samplingThread, SLOT( setAmplitude( double ) ) );
window.connect( &window, SIGNAL( signalIntervalChanged( double ) ),
    &samplingThread, SLOT( setInterval( double ) ) );

window.show();

samplingThread.start();
window.start();

bool ok = app.exec();

samplingThread.stop();
samplingThread.wait( 1000 );

return ok;
}

above's window.start() is equal to plot->start(). and i can not find the linkage between plot object and samplingthread object. Can anyone explain this part for me?


Solution

  • There is a use of a curious singleton pattern based on the class SignalData. First of all, the singleton :

    class CurveData: public QwtSeriesData<QPointF>
    {
    public:
        const SignalData &values() const;
        SignalData &values();
    ...
    };
    
    const SignalData &CurveData::values() const
    {
        return SignalData::instance();
    }
    
    QPointF CurveData::sample( size_t i ) const
    {
        return SignalData::instance().value( i );
    }
    

    On one side

    Plot::Plot( QWidget *parent ):
    {
        d_curve = new QwtPlotCurve();
        ...
        d_curve->setData( new CurveData() ); //singleton being used inside each curvedata
        ...
    }
    

    And on the other

    void SamplingThread::sample( double elapsed )
    {
        if ( d_frequency > 0.0 )
        {
            const QPointF s( elapsed, value( elapsed ) );
            SignalData::instance().append( s ); //singleton being used
        }
    }
    

    I'll refrain from using this example as it is.