【问题标题】:Disable validation in QSpinBox在 QSpinBox 中禁用验证
【发布时间】:2014-03-24 13:02:50
【问题描述】:

因此我有一个 QSpinBox,并且想要取消设置验证,不仅可以写入 int 值,还可以写入字符串。 请帮我解决这个问题。 这个我试过了,还是不行:

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 validation qspinbox


    【解决方案1】:

    Qt 文档说:

    如果 lineEdit 的 QLineEdit::validator() 返回 0,则内部 spinbox 的验证器将在行编辑时设置。

    因此,为了禁用 QSpinBox 的内置验证器,您需要设置自己的(虚拟?)。即

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

    【讨论】:

    • @EduardRostomyan 使用您自己的验证器而不是 QIntValidator。
    • @EduardRostomyan,我更新了我的答案并添加了一个虚拟验证器,允许在行编辑中设置任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 2014-10-14
    相关资源
    最近更新 更多