Qt:Subclassed spinbox不计算在内

时间:2016-02-07 20:32:42

标签: c++ qt

我尝试将Qt旋转框子类化,以便我可以获得鼠标事件。代码编译,事件正在运行,但是spinbox不会向上或向下计数。我将最小值设置为-9999,最大值设置为9999,默认值为100.但是如果单击向上或向下按钮,则没有任何反应。

NewSpinBox.h:

   #ifndef NEWSPINBOX_H
   #define NEWSPINBOX_H

   #include <QObject>
   #include <QWidget>
   #include <QEvent>
   #include <QMouseEvent>
   #include <QSpinBox>

   class NewSpinBox : public QSpinBox
   {
        Q_OBJECT
    public:
        NewSpinBox(QWidget *parent = 0);
        ~NewSpinBox();
    public slots:
        void mousePressEvent(QMouseEvent *mouseEvent);
        void mouseReleaseEvent(QMouseEvent *mouseEvent);
   };

   #endif // NEWSPINBOX_H

NewSpinBox.cpp:

#include "newspinbox.h"

NewSpinBox::NewSpinBox(QWidget *parent) : QSpinBox(parent)
{

}

NewSpinBox::~NewSpinBox()
{

}


void NewSpinBox::mousePressEvent(QMouseEvent *mouseEvent)
{
    if(mouseEvent->type() == QMouseEvent::MouseButtonRelease)
    {
        int i = 0;
        i++;
    }
}

void NewSpinBox::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
    if(mouseEvent->type() == QMouseEvent::MouseButtonRelease)
    {
        int i = 0;
        i++;
    }
}

我是否必须重新实现计数功能?

1 个答案:

答案 0 :(得分:0)

您必须调用基类处理程序以获取默认行为:

void NewSpinBox::mousePressEvent(QMouseEvent *mouseEvent)
{
    QSpinBox::mousePressEvent(mouseEvent);
    // ... your code
}

void NewSpinBox::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
    QSpinBox::mouseReleaseEvent(mouseEvent);
    // ... your code
}