[Qt-interest] Customizing QSpinBox in Qt4

Malyushytsky, Alex alex at wai.com
Mon Jun 29 23:08:11 CEST 2009


>> This works. What I dislike is, that I have to subclass the spin box to change something I consider to be a property.

Why do you dislike an idea of subclassing?
The only thing came to my mind it was inconvenience of using QDesigner with subclasses in Qt3
(creating and managing addons added extra complexity) .
Qt4 added "Promoting Widgets" feature which fixed that problem.

Regards,
   Alex



-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Lars Amsel
Sent: Sunday, June 28, 2009 11:24 PM
To: qt-interest at trolltech.com
Subject: [Qt-interest] Customizing QSpinBox in Qt4

Hi,

I want to customize the behaviour of QSpinBox. When the user enters a
number directly, numbers outside the range of QSpinBox should be
allowed. They should be truncated to min/max value, when the edit submits.

I managed to do this with this class:

class TolerantSpinBox : public QSpinBox {
public:
   TolerantSpinBox(QWidget* parent = NULL);
protected:
   virtual QValidator::State validate(QString& input, int& pos) const;
   virtual void fixup(QString& input) const;
;

TolerantSpinBox::TolerantSpinBox(QWidget* parent) : QSpinBox(parent) {
}

QValidator::State TolerantSpinBox::validate(QString& input, int& pos)
const {
   QValidator::State result = QSpinBox::validate(input, pos);
   if (result == QValidator::Invalid) {
     bool ok = false;
     input.toInt(&ok, 10);
     if (ok) {
       result = QValidator::Intermediate;
     }
   }
   return result;
}

void TolerantSpinBox::fixup(QString& input) const {
   bool ok;
   int value = input.toInt(&ok, 10);
   if (ok) {
     value = qMin(qMax(value, minimum()), maximum());
     input = QString::number(value);
   } else {
     QSpinBox::fixup(input);
   }
}

This works. What I dislike is, that I have to subclass the spin box to
change something I consider to be a property. In previous versions of Qt
(3.x AFAIK) there was a setValidator-method, which offered the same
functionality. This method seems to be gone with Qt4. Is there a way to
get the desired behaviour without subclassing QSpinBox?

thanks

Lars
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."

"Please consider our environment before printing this email."




More information about the Qt-interest-old mailing list