William RENOU
William RENOU

Reputation: 41

Inheritence and Qt signals

Edit : So now I have to make QLCDNumber clickable. Clicking it needs to reset m_Count. I added a class myLCDNumber inheriting from QLCDNumber. All it does is redefine mouseReleaseEvent(QMouseEvent *e) to use a custom signal received by a ButtonCount instance, which sets m_Count to 0. But it does not work (again). Looks like he can't find the clicked() signal though it's defined in the myLCDNumber class.

myLCDNumber.h

#ifndef MYLCDNUMBER_H
#define MYLCDNUMBER_H

#include <iostream>
#include <QObject>
#include <QApplication>
#include <QPushButton>
#include <QLCDNumber>

class myLCDNumber : public QLCDNumber
{
    public:
        myLCDNumber(uint numDigits);
        ~myLCDNumber();
        void mouseReleaseEvent(QMouseEvent *e);

    signals:
        void clicked();
};

#endif // MYLCDNUMBER_H

myLCDNumber.cpp

#include <QMouseEvent>
#include "mylcdnumber.h"

myLCDNumber::myLCDNumber(uint numDigits):QLCDNumber(numDigits){}

myLCDNumber::~myLCDNumber(){}

void myLCDNumber::mouseReleaseEvent(QMouseEvent *e)
{
    qDebug("je suis bien dans le click");
    if (e->button() == Qt::LeftButton)
        emit clicked();
}

I'm supposed to make a ButtonCount class, based on QPushButton. All it does is counting the number of times it has been used (through a variable inside the class). Clicking it increments the m_Count variable (through ButtonCount.Increment()), then sends a valueChanged signal (with the m_Count variable as value) to a display() slot on a QLCDNumber widget

For some reason, clicking on the button does not increase the LCD counter as intended. I think the m_Count is correctly modified each time the button is clicked, but I can't verify it (no debug avaible).

ButtonCount.h

#ifndef BUTTONCOUNT_H
#define BUTTONCOUNT_H

#include <iostream>
#include <QObject>
#include <QApplication>
#include <QPushButton>

class ButtonCount : public QPushButton
{
    Q_OBJECT

    public:
        ButtonCount(const QString & text, QWidget * parent = 0);
        ~ButtonCount();
        int m_Count;

    public slots:
        void Increment();

    signals:
        void valueChanged(int);
};

#endif // BUTTONCOUNT_H

ButtonCount.cpp

#include "buttoncount.h"
    ButtonCount::ButtonCount(const QString &text, QWidget *parent):QPushButton(text, parent)
    {
        m_Count = 0;
        QObject::connect(this,SIGNAL(clicked()),this,SLOT(Increment()));
    }

    ButtonCount::~ButtonCount(){}

    void ButtonCount::Increment(){
        m_Count++;
        emit valueChanged(m_Count);
    }

main.cpp

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QLCDNumber>
#include <QLayout>
#include <QDial>
#include "buttoncount.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget window;

    ButtonCount *button_c = new ButtonCount("Count clicks");
    myLCDNumber *lcd = new myLCDNumber(2);
    lcd->setSegmentStyle(QLCDNumber::Filled);
    QObject::connect(button_c,SIGNAL(valueChanged(int)),lcd,SLOT(display(int)));
    QObject::connect(lcd,SIGNAL(clicked()),button_c,SLOT(reset()));
    QVBoxLayout *layout = new QVBoxLayout();

    layout->addWidget(lcd);
    layout->addWidget(button_c);

    window.setLayout(layout);

    window.show();

    return a.exec();
}

Upvotes: 0

Views: 468

Answers (1)

Marek R
Marek R

Reputation: 38181

Why inheritance? This is bad design. Simple object of new type do the job nicely:

class ButtonCounter : public QObject {
    Q_OBJECT

    int mCounter;
public:
    Q_PROPERTY(int counter
               READ counter 
               WRITE setCounter
               NOTIFY counterChanged )

    explicit ButtonCounter(QObject *parent) : 
         QObject(parent),
         mCounter(0) {
    }

    explicit ButtonCounter(QAbstractButton *parent) : 
         QObject(parent),
         mCounter(0) {
        connect(parent, SIGNAL(clicked()), this, SLOT(countOne()));
    }
    int counter() const {
        return mCounter;
    }

public slots:
    void setCounter(int value) {
       if (mCounter != value) {
           mCounter = value;
           emmit counterChanged(mCounter);
       }
    }

    void countOne() {
       setCounter(mCounter+1);
    }

public signals:
    void counterChanged(int newValue);
};

Upvotes: 1

Related Questions