【问题标题】:How to change the focus of QLineEdit automatically to another QLineEdit after input satisfy a criterion?输入满足条件后如何将 QLineEdit 的焦点自动更改为另一个 QLineEdit?
【发布时间】:2016-10-21 22:01:39
【问题描述】:

我有两个 QLineEdit 小部件,edt1edt2。每个 QLineEdit 只能接受两位数。我在edt1中输入xx(如10)后,可以满足输入条件,如何自动将焦点从edt1更改为edt2

是否有内置函数可以用来实现这一点?或者,任何人都可以提供有关如何执行此操作的信息吗?谢谢。

【问题讨论】:

    标签: qt focus qlineedit


    【解决方案1】:

    每次发出textChanged()信号时,您都需要检查edt1.hasAcceptableInput(),如果是,则调用edt2.setFocus()

    #include <QtWidgets>
    
    int main(int argc, char** argv)
    {
        QApplication a{argc, argv};
    
        QWidget w;
        QLineEdit lineEdit1;
        QLineEdit lineEdit2;
        //validator to accept two digits
        QRegExpValidator validator{QRegExp{"\\d{2}"}};
        lineEdit1.setValidator(&validator);
        lineEdit2.setValidator(&validator);
        QVBoxLayout layout{&w};
        layout.addWidget(&lineEdit1);
        layout.addWidget(&lineEdit2);
        w.show();
    
        QObject::connect(&lineEdit1, &QLineEdit::textChanged, [&](){
            if(lineEdit1.hasAcceptableInput())
                lineEdit2.setFocus();
        });
    
        return a.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      相关资源
      最近更新 更多