Sine Wave Generator Application in QT C++
Rate this article
5.00 out of 5
In this Video Tutorial we will show you how to develop a Sine Wave Generator application in QT C++.

Sine wave is one of fundamental periodic oscillating mode of any system. For example all electrical inverters are characterized by the Amplitude quality of sine wave generated as output.
 
Watch the video tutorial from the link below..




Main Source Code

Multithread.h


#ifndef MULTITHREAD_H
#define MULTITHREAD_H
#include <QObject>
#include <QMutex>
#include <QWaitCondition>
class MultiThread : public QObject
{
    Q_OBJECT

public:
     MultiThread(QObject *parent = 0);
     void requestThread();
     void abort();
     void resume();

private:

    bool _abort;
    bool _working;
    QMutex mutex;
    QWaitCondition pauseContd;

signals:

    void multithreadRequested();
    void valueChanged(const QString &value);
    void finished();
    void resumed();
    void value();

public slots:

    void doMultiThraed();
};
#endif // MULTITHREAD_H

Multithread.cpp



#include "multithread.h"
#include <QTimer>
#include <QEventLoop>
#include <QThread>
#include <QDebug>

MultiThread::MultiThread(QObject *parent) :
    QObject(parent)
{
    _working =false;
    _abort = false;
}

void MultiThread::requestThread()
{
    mutex.lock();
    _working = true;
    _abort = false;
    qDebug()<<thread()->currentThreadId();
    mutex.unlock();

    emit multithreadRequested();
}

void MultiThread::abort()
{
    mutex.lock();
    if (_working) {
        _abort = true;
        qDebug()<<thread()->currentThreadId();
    }
    mutex.unlock();
}
void MultiThread::resume()
{
mutex.lock();
if(!_working)
{
_abort = true;
qDebug()<<thread()->currentThreadId();
}
mutex.unlock();
pauseContd.wakeAll();
emit resume();


}

void MultiThread::doMultiThraed()
{
    qDebug()<<thread()->currentThreadId();


    long int iCounter=0;
    while(true)
    {
        iCounter++;
        int a=iCounter;
        //emit
        if(a!=0)
                {
                   emit value();
                }
        mutex.lock();
        bool abort = _abort;
        mutex.unlock();

        if (abort) {
            qDebug()<<thread()->currentThreadId();
            break;
        }

        QEventLoop loop;
        QTimer::singleShot(500, &loop, SLOT(quit()));
        loop.exec();
        emit valueChanged(QString::number(iCounter));
        if(iCounter==5)
        {
            iCounter=0;
        }
    }
    mutex.lock();
    _working = false;
    mutex.unlock();

    qDebug()<<thread()->currentThreadId();

    emit finished();
}

SineWave.h

#ifndef SINWAVE_H
#define SINWAVE_H

#include <QMainWindow>
#include "multithread.h"

namespace Ui {
    class Sinwave;
}

class Sinwave : public QMainWindow {
    Q_OBJECT
public:

    qreal amplitude;
    qreal Ttime;
    qreal X1; // X Cordinate
    qreal Y1; // Y Cordinate
    int sinCount;
    int lbl1;


    Sinwave(QWidget *parent = 0);
    ~Sinwave();

protected:
    void changeEvent(QEvent *e);
    void paintEvent(QPaintEvent *);

private:
    Ui::Sinwave *ui;
    QThread *thread;
    MultiThread *multithread;

private slots:
    void on_actionPause_activated();
    //void on_actionThreadStart_activated();
    void on_actionStart_activated();
    void on_actionSTART_activated();
    void on_pushButtonStart_clicked();
    void display();
};

#endif // SINWAVE_H

Sinewave.cpp

#include "sinwave.h"
#include "ui_sinwave.h"
#include "multithread.h"
#include <QtGui>
#include <QPalette>
#include <cmath>
#include <QPainterPath>
#include <QPainter>
#include <QThread>
#include <QDebug>


static int i=0;
static int XStartpoint=0;
static int YStartpoint=0;
static int jCount=5;

Sinwave::Sinwave(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Sinwave)
{
    ui->setupUi(this);
    thread = new QThread();
    multithread = new MultiThread();

    multithread->moveToThread(thread);
    connect(multithread, SIGNAL(valueChanged(QString)), ui->labelCount, SLOT(setText(QString)));
    connect(multithread, SIGNAL(multithreadRequested()), thread, SLOT(start()));
    connect(thread, SIGNAL(started()), multithread, SLOT(doMultiThraed()));
    connect(multithread, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
    // receive the emited signal
    connect(multithread, SIGNAL(value()),SLOT(display()));

}

Sinwave::~Sinwave()
{
    multithread->abort();
    thread->wait();
    qDebug()<<this->QObject::thread()->currentThreadId();
    delete thread;
    delete multithread;
    delete ui;
}

void Sinwave::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Sinwave::paintEvent(QPaintEvent *pEvent)
{

    QWidget::paintEvent(pEvent);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::red);
    painter.drawLine(100,200,100,600);
    painter.drawLine(100,400,750,400);

    painter.drawText(630,430,"-> Time X axis");
    painter.drawText(70,250,"^");
    painter.drawText(73,252,"|");
    painter.drawText(80,255,"A");

    //arrows

    painter.drawLine(750,400,740,390);
    painter.drawLine(750,400,740,410);

    painter.drawLine(100,200,110,210);
    painter.drawLine(100,200,90,210);



    QPainterPath path1;
    path1.moveTo(XStartpoint,YStartpoint);
    qreal tempAmp;

    for(int SS=0;SS<sinCount;SS++)
    {
        tempAmp=amplitude;
        for(int a=1;a<amplitude;a++)
        {
            X1+=Ttime/tempAmp;
            tempAmp--;
            Y1-=1;

            path1.lineTo(X1,Y1);
            path1.moveTo(X1,Y1);
        }
        tempAmp=1;
        for(int g=amplitude;g>0;g--)
        {
            X1+=Ttime/tempAmp;
            tempAmp++;
            Y1+=1;

            path1.lineTo(X1,Y1);
            path1.moveTo(X1,Y1);
        }
// second

        tempAmp=amplitude;
        for(int a=1;a<amplitude;a++)
        {
           X1+=Ttime/tempAmp;
           tempAmp--;
           Y1+=1;

           path1.lineTo(X1,Y1);
           path1.moveTo(X1,Y1);
        }
        tempAmp=1;
        for(int g=amplitude;g>0;g--)
        {
            X1+=Ttime/tempAmp;
            tempAmp++;
            Y1-=1;

            path1.lineTo(X1,Y1);
            path1.moveTo(X1,Y1);
        }
    }
    painter.drawPath(path1);
}




void Sinwave::on_actionSTART_activated()
{

}

void Sinwave::on_actionStart_activated()
{
    // code to start multithread
    multithread->abort();
    thread->wait();
    multithread->requestThread();
}


void Sinwave::on_actionPause_activated()
{
    // Pause mulithread
    multithread->abort();
    thread->wait();
}

void Sinwave::display()
{

    QString amp=ui->textEditAmplitude->toPlainText();
    QString tym=ui->textEditTime->toPlainText();
    amplitude=amp.toInt();
    Ttime=tym.toInt();
    X1=100.0;
    Y1=400.0;
    XStartpoint=100;
    YStartpoint=400;
    QString lbl=ui->labelCount->text();
    lbl1=lbl.toInt();
    if(lbl1==1)
    {

        sinCount=1;
        repaint();
    }
    else if(lbl1==2)
    {
        sinCount=2;
        repaint();
    }
    if(lbl1==3)
    {
        sinCount=3;
        repaint();
    }
    if(lbl1==4)
    {
        sinCount=4;
        repaint();
    }
    if(lbl1==5)
    {
        sinCount=5;
        repaint();
    }

}

void Sinwave::on_pushButtonStart_clicked()
{
    // copy code to generate sin wave
       amplitude=0.0;
       Ttime=0.0;
       X1=100.0;
       Y1=400.0;
       QString amp=ui->textEditAmplitude->toPlainText();
       QString tym=ui->textEditTime->toPlainText();
       amplitude=amp.toInt();
       Ttime=tym.toInt();
       XStartpoint=100;
       YStartpoint=400;
       sinCount=5;
       repaint();
}





Download

   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments
   Design  HTML

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud