我想在 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