【问题标题】:Qt QSpinbox accepting '+' symbol after setting rangeQt QSpinbox在设置范围后接受'+'符号
【发布时间】:2018-09-21 04:16:24
【问题描述】:

我有一个QSpinbox,我正在设置它的范围:

QSpinBox *SpinBox = new QSpinBox;
SpinBox->setRange(0, 100);

但是,我可以手动输入 + 符号,该符号未反映在我的广告位中。

connect (SpinBox, SIGNAL (valueChanged (QString)), this,
            SLOT (onSpinBoxChanged (QString)));

我也尝试用int 替换QString。但是+ 并没有反映在槽中。

如何限制输入+ 符号?

我已经提到了一些 Qt 和 StackOverflow 的帖子/答案,这些帖子/答案是在旋转框中禁用行编辑:

我尝试制作旋转框的行编辑 ReadOnly,但我无法这样做,因为它是一个 const 变量。

一些答案​​建议继承QSpinbox 类。

还有其他方法可以限制+ 符号或禁用QSpinbox 的行编辑本身吗?

【问题讨论】:

    标签: c++ qt qt5 qspinbox


    【解决方案1】:

    如果您不想从QSpinBox 类继承,一个可能的解决方案是使用eventFilter,在下面的代码中我展示了一个示例:

    #include <QApplication>
    #include <QSpinBox>
    #include <QLineEdit>
    #include <QKeyEvent>
    
    class PlusRemoveHelper: public QObject{
    public:
        using QObject::QObject;
        void addWidget(QWidget *widget){
            if(widget){
                widgets.append(widget);
                widget->installEventFilter(this);
            }
        }
    public:
        bool eventFilter(QObject *watched, QEvent *event) override
        {
            if(std::find(widgets.begin(), widgets.end(), watched) != widgets.end()
                    && event->type() == QEvent::KeyPress){
                QKeyEvent *keyevent = static_cast<QKeyEvent *>(event);
                if(keyevent->text() == "+")
                    return true;
            }
            return  QObject::eventFilter(watched, event);
        }
    private:
        QWidgetList widgets;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QSpinBox w;
        w.setRange(0, 100);
        PlusRemoveHelper helper;
        helper.addWidget(&w);
        w.show();
    
        return a.exec();
    }
    

    如果你在一个小部件中,你可以实现相同的逻辑:

    *.h

    ...
    class QSpinBox;
    
    class SomeClass: public SuperClass
    {
    ...
    public:
        bool eventFilter(QObject *watched, QEvent *event);
    private:
        ...
        QSpinBox *SpinBox
    };
    

    *.cpp

    SomeClass::SomeClass(...):
      SuperClass(..)
    {
        SpinBox = new QSpinBox;
        SpinBox->setRange(0, 100);
        SpinBox->installEventFilter(this):
    }
    
    
    bool SomeClass::eventFilter(QObject *watched, QEvent *event){
        if(watched == SpinBox && event->type() == QEvent::KeyPress){
            QKeyEvent *keyevent = static_cast<QKeyEvent *>(event);
            if(keyevent->text() == "+")
                return true;
        }
        return  SomeClass::eventFilter(watched, event);
    }
    

    【讨论】:

    • 感谢@eyllanesc 的详细回答..我尝试了您的第一个示例..我尝试了一个 ui 表单 spinbox:ui->spinBox->setRange(0, 100); PlusRemoveHelper helper;helper.addWidget((ui->spinBox)); 但它仍然接受“+”号。我在表单中遗漏了什么吗?
    • @george 如果您意识到 helper 将是一个局部变量,在创建它的函数完成时将被删除,解决方案是在堆中创建它以将其更改为:ui-&gt;spinBox-&gt;setRange(0, 100); PlusRemoveHelper* helper = new PlusRemoveHelper(this); helper-&gt;addWidget(ui-&gt;spinBox);
    • 谢谢@eyllanesc..我认为第一个解决方案适合我的情况,因为我有大约 10 到 20 个旋转框。所以将它们全部添加到 QWidgetList 应该可以它们都限制了 '+' 符号,不管它们是在哪个类中创建的,对吗?
    猜你喜欢
    • 2019-03-13
    • 2013-04-24
    • 2021-08-14
    • 2011-09-11
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多