Qt窗体中含有QLineEdit的事件触发情况下的注意事项

点击窗体上的确定、取消等其它按钮时,按钮的槽函数会先于QLineEdit的editingFinished()信号

这就会导致许多问题,比如点击关闭按钮后,窗体的closeEvent被重写成隐藏,那么QLineEdit的focusOutEvent事件触发editingFinished()信号,这个信号连接的槽函数用于检查QLineEdit的合法性,不合法则弹出提示窗体,这样QLineEdit的focusOutEvent又会被触发。。。后面就是无限循环啦(这时候的你哭把T_T)

解决方案:自定义一个QLineEdit,并重写focusOutEvent

class MyLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    MyLineEdit(QWidget * parent = 0) : QLineEdit(parent) {}
    ~MyLineEdit(){}

protected:
    virtual void focusOutEvent(QFocusEvent * e)
    {
        if (e->reason() == Qt::ActiveWindowFocusReason)
        {
            return;
        }
        else
        {
            QLineEdit::focusOutEvent(e);
        }
    }
};

你可能感兴趣的:(Qt)