编辑:
所以现在我必须使QLCDNumber可单击。单击它需要重置m_Count。
我添加了一个从QLCDNumber继承的myLCDNumber类。它所做的只是重新定义mouseReleaseEvent(QMouseEvent * e)以使用由ButtonCount实例接收的自定义信号,该实例将m_Count设置为0。
但是(再次)它不起作用。看起来他找不到myLCDNumber类中定义的clicked()信号。
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();
}
我应该基于QPushButton制作一个ButtonCount类。
它所做的只是计算使用的次数(通过类中的变量)。
单击它会增加m_Count变量(通过ButtonCount.Increment()),然后将valueChanged信号(将m_Count变量作为值)发送到QLCDNumber小部件上的display()插槽
出于某些原因,单击按钮不会按预期增加LCD计数器。我认为每次单击按钮都会正确修改m_Count,但是我无法验证(没有调试可用)。
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();
}
最佳答案
为什么要继承?这是糟糕的设计。新型的简单对象可以很好地完成工作:
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);
};