質問

Hence I have a QSpinBox, and want to unset validation for writing not only int values, but also string in it. Please help me to fix this. I have tried this, but it does not work:

class Spinbox:public QSpinBox
{
public:

    Spinbox(QWidget* parent=0)
        :QSpinBox(parent){}
    void setLineEdit(QLineEdit *l)
    {
        QSpinBox::setLineEdit(l);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Spinbox de;
    QLineEdit le;
    le.setValidator(0);
    le.setText("text");
    de.setLineEdit(&le);
    de.show();

    return a.exec();
}
役に立ちましたか?

解決

Qt docs say that:

If QLineEdit::validator() for the lineEdit returns 0, the internal validator of the spinbox will be set on the line edit.

Thus, in order to disable the QSpinBox's built in validator, you need to set your own (dummy?). I.e.

class Validator : public QValidator
{
public:
    State validate(QString &input, int &pos ) const
    {
        return QValidator::Acceptable;
    }
};

[..]

Spinbox de;
QLineEdit le;
le.setValidator(new Validator());
le.setText("text");
de.setLineEdit(&le);
de.show();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top