Search code examples
c++qtqspinboxqt5.12

valueChanged(const Qstring &) not fired in qspinbox when empty


I'm having a QSpinbox inside a QTreeWidget following an approach similar to the approach for a combobox described here https://stackoverflow.com/a/4849010/10220019.

I've read here https://forum.qt.io/topic/103667/qdoublespinbox-why-isn-t-valuechanged-signal-fired-when-content-is-empty/5 that if you use the valueChanged(const QString&)signal this should fire when you backspace from a number to empty. However this is not the case for me. The only difference I can see between that post and my code is that I use a QSpinBox and not a QDoubleSpinBox but that shouldn't be the problem I guess.

I think I might be connecting the signal wrongly because the behavior I get completely matches the behavior I would expect from valueChanged(int). But I wouldn't know how to write the connect then. Does somebody see the error?

My code is: QTreeWidgetItemSpinBox.h

#pragma once

#pragma warning (push)
#pragma warning (disable: 26451 26495 26498 26439)
#include <QtWidgets/QSpinbox>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QToolTip>
#pragma warning (pop)

class QTreeWidgetItemSpinBox :
    public QSpinBox
{
    Q_OBJECT

public:
    QTreeWidgetItemSpinBox(QTreeWidgetItem* treeItem, int column);

public slots:
    void validateValue(const QString& input);

private:
    QTreeWidgetItem* treeItem;
    int column;
};

QTreeWidgetItemSpinBox.cpp

#include "QTreeWidgetItemSpinBox.h"
#include <QtCore/QDebug>

QTreeWidgetItemSpinBox::QTreeWidgetItemSpinBox(QTreeWidgetItem* treeItem, int column)
    : treeItem(treeItem), column(column)
{
    connect(this, SIGNAL(valueChanged(const QString&)), SLOT(validateValue(const QString&)));
}

void QTreeWidgetItemSpinBox::validateValue(const QString& input)
{
    qDebug() << "Called"; //Is called when changed to another number but not if the change is to empty or if I add zeros before a number
}

Solution

  • I'm not sure why that Qt forums post says QDoubleSpinBox::valueChanged(const QString &) is emitted when the value is blank because it is not (as of Qt 5.12 anyway). Both versions of the signal are emitted consecutively from the same place in the code. Maybe it was different in a previous version. Same for QSpinBox. I guess the reasoning is that since the value is invalid, it hasn't actually changed (because eg. if you lose focus/finish editing right then, it will auto-revert to the last known good value... so the value hasn't actually changed).

    You could instead connect to the QLineEdit (obtained with QAbstractSpinBox::lineEdit()) textChanged() (or textEdited()) signal to get the actual value of the editor.