Details
-
Suggestion
-
Resolution: Out of scope
-
P3: Somewhat important
-
None
-
None
Description
The valueChanged signal of the various QAbstractSpinBox subclasses is only emitted for valid input. This makes it impossible through public APIs to disable ie. next- or ok-buttons in dialogs on invalid input. Q*SpinBox should propagate the validation state of the input through a signal to avoid the need to use workarounds like this:
#include <QtGui> class Dialog : public QDialog { Q_OBJECT public: Dialog() : isValid(true) { spinBox = new QSpinBox; spinBox->setMinimum(32); spinBox->setMaximum(1024); QHBoxLayout *hbox = new QHBoxLayout; hbox->addWidget(spinBox); setLayout(hbox); connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(mySlot(int))); if (QLineEdit *lineEdit = qFindChild<QLineEdit*>(spinBox)) { connect(lineEdit, SIGNAL(textEdited(QString)), this, SLOT(mySlot(QString))); } } protected: void closeEvent(QCloseEvent *e) { if (!isValid) e->ignore(); } private slots: void mySlot(int value) { qDebug() << value; } void mySlot(const QString &value) { int pos = 0; QString copyValue = value; QAbstractSpinBox *aSpinBox = spinBox; isValid = aSpinBox->validate(copyValue, pos) == QValidator::Acceptable; } private: QSpinBox *spinBox; bool isValid; }; #include "main.moc" int main(int argc, char *argv[]) { QApplication app(argc, argv); Dialog dialog; dialog.show(); return app.exec(); }