按钮上的脉冲LED在QT中切换

时间:2016-09-20 11:01:42

标签: c++ qt user-interface led

我正在学习Qt,我想出了一个用按钮打开和关闭LED的想法,我让它工作但我无法弄清楚如何使用切换按钮来实现它。我将在下面发布代码,请看一下,让我知道如何实现上述行为。

gif showing the behavior

pulseledwidget.h

#ifndef PULSINGLEDWIDGET_H
#define PULSINGLEDWIDGET_H

#include <QLabel>
#include <QPixmap>
#include <QTimer>
#include <QObject>
#include <QDebug>

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

pulseledwidget.cpp

#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(&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);
}

leddialog.h

#ifndef LEDDIALOG_H
#define LEDDIALOG_H

#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#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

leddialog.cpp

#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->setAlignment(Qt::AlignCenter);

    layout->addWidget(ledwidget);
    layout->addWidget(onbutton);
    layout->addWidget(offbutton);

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

}

我尝试了什么:

leddialog.h

#ifndef LEDDIALOG_H
#define LEDDIALOG_H

#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#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

leddialog.cpp

#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->setAlignment(Qt::AlignCenter);

    onbutton->setCheckable(true);

    layout->addWidget(ledwidget);
    layout->addWidget(onbutton);
    layout->addWidget(offbutton);

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

}

void ledDialog::makeDecision(bool state)
{
    if(state)
    {
        ledwidget->startPulsing();
    }
    else
    {
        ledwidget->extinguish();
    }
}

但上述程序无效。我试图在调试模式下运行它以了解我所犯的错误,它显示了&#34;分段错误&#34;错误。

0 个答案:

没有答案