在失去对lineedit1的关注之后,我想做一些具体的事情。这是来自应用程序的代码段:

QFocusEvent* focuslost = new QFocusEvent(QEvent::FocusOut);
void lineedit1::focusOutEvent(focuslost) {
  //do something
}


但这给了我编译器错误:


  
    错误:名称后跟“ ::”必须是类或名称空间名称
  


我该怎么做才能实现这样的功能?

最佳答案

您不能只覆盖对象实例上的方法,而是需要创建一个继承自QLineEdit并覆盖方法focusOutEvent的新类:

mylineedit.h

class MyLineEdit : public QLineEdit
{
protected:
    void focusOutEvent(QFocusEvent * e) override;
}


mylineedit.cpp

void MyLineEdit::focusOutEvent(QFocusEvent * e)
{
    // your code here

    QLineEdit::focusOutEvent(e);
}


然后将lineedit1声明为MyLineEdit* lineedit1

09-03 22:04