Pulse LED on button toggle in QT
<p>I am learning Qt and i came up with an idea to switch on and off an LED with buttons, i got it working but i couldn't figure out how to implement the same with a toggle button. I will be posting the code below, please take a look and let me know how i can implement the above stated behavior.</p>

<p><a href="https://i.stack.imgur.com/9W7mh.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9W7mh.gif" alt="gif showing the behavior"></a></p>

<p><strong>pulseledwidget.h</strong></p>

<pre><code>#ifndef PULSINGLEDWIDGET_H
#define PULSINGLEDWIDGET_H

#include &lt;QLabel&gt;
#include &lt;QPixmap&gt;
#include &lt;QTimer&gt;
#include &lt;QObject&gt;
#include &lt;QDebug&gt;

class pulsingLedWidget : public QLabel
{
    Q_OBJECT
public:
    explicit pulsingLedWidget(QWidget *parent = 0);
public slots:
    void startPulsing();
    void extinguish();
private:
    QPixmap onPixmap,offPixmap;
    QTimer timer;
    bool state;
};

#endif // PULSINGLEDWIDGET_H
</code></pre>

<p><strong>pulseledwidget.cpp</strong></p>

<pre><code>#include "pulsingledwidget.h"

pulsingLedWidget::pulsingLedWidget(QWidget *parent) : QLabel(parent)
{
    onPixmap.load(":/ledon.png");
    offPixmap.load(":/ledoff.png");

    state = true;

    setPixmap(offPixmap);
    timer.setInterval(200);
    connect(&amp;timer,SIGNAL(timeout()),this,SLOT(startPulsing()));
}

void pulsingLedWidget::startPulsing()
{
    timer.start();
    if(state)
    {
        setPixmap(onPixmap);
        state = false;
    }
    else
    {
        setPixmap(offPixmap);
        state = true;
    }
}

void pulsingLedWidget::extinguish()
{
    timer.stop();
    setPixmap(offPixmap);
}
</code></pre>

<p><strong>leddialog.h</strong></p>

<pre><code>#ifndef LEDDIALOG_H
#define LEDDIALOG_H

#include &lt;QDialog&gt;
#include &lt;QVBoxLayout&gt;
#include &lt;QPushButton&gt;
#include "pulsingledwidget.h"

class ledDialog : public QDialog
{
    Q_OBJECT
public:
    explicit ledDialog(QWidget *parent = 0);
private:
    QPushButton *onbutton;
    QPushButton *offbutton;
    QVBoxLayout *layout;
    pulsingLedWidget *ledwidget;
};

#endif // LEDDIALOG_H
</code></pre>

<p><strong>leddialog.cpp</strong></p>

<pre><code>#include "leddialog.h"

ledDialog::ledDialog(QWidget *parent) : QDialog(parent)
{
    QPushButton *onbutton = new QPushButton("On");
    QPushButton *offbutton = new QPushButton("Off");
    QVBoxLayout *layout = new QVBoxLayout(this);
    pulsingLedWidget *ledwidget = new pulsingLedWidget(this);

    ledwidget-&gt;setAlignment(Qt::AlignCenter);

    layout-&gt;addWidget(ledwidget);
    layout-&gt;addWidget(onbutton);
    layout-&gt;addWidget(offbutton);

    resize(150,200);
    connect(onbutton,SIGNAL(clicked(bool)),ledwidget,SLOT(startPulsing()));
    connect(offbutton,SIGNAL(clicked(bool)),ledwidget,SLOT(extinguish()));

}
</code></pre>

<p><strong>What i have tried :</strong> </p>

<p><strong>leddialog.h</strong></p>

<pre><code>#ifndef LEDDIALOG_H
#define LEDDIALOG_H

#include &lt;QDialog&gt;
#include &lt;QVBoxLayout&gt;
#include &lt;QPushButton&gt;
#include "pulsingledwidget.h"

class ledDialog : public QDialog
{
    Q_OBJECT
public:
    explicit ledDialog(QWidget *parent = 0);
private slots:
    void makeDecision(bool state);
private:
    QPushButton *onbutton;
    QPushButton *offbutton;
    QVBoxLayout *layout;
    pulsingLedWidget *ledwidget;
};

#endif // LEDDIALOG_H
</code></pre>

<p><strong>leddialog.cpp</strong></p>

<pre><code>#include "leddialog.h"

ledDialog::ledDialog(QWidget *parent) : QDialog(parent)
{
    QPushButton *onbutton = new QPushButton("On");
    QPushButton *offbutton = new QPushButton("Off");
    QVBoxLayout *layout = new QVBoxLayout(this);
    pulsingLedWidget *ledwidget = new pulsingLedWidget(this);

    ledwidget-&gt;setAlignment(Qt::AlignCenter);

    onbutton-&gt;setCheckable(true);

    layout-&gt;addWidget(ledwidget);
    layout-&gt;addWidget(onbutton);
    layout-&gt;addWidget(offbutton);

    resize(150,200);
connect(onbutton,SIGNAL(toggled(bool)),this,SLOT(makeDecision(bool)));

}

void ledDialog::makeDecision(bool state)
{
    if(state)
    {
        ledwidget-&gt;startPulsing();
    }
    else
    {
        ledwidget-&gt;extinguish();
    }
}
</code></pre>

<p>but the above procedure is not working. I tried to run it in debug mode to know the mistake that i have made, it is showing "Segmentation fault" error.</p>