Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-2588

Add a signal to QAbstractSpinBox that informs about invalid input

    XMLWordPrintable

Details

    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();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bjnilsen Bjørn Erik Nilsen
            vhilshei Volker Hilsheimer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes