如何在Qt中获取QLineEdit的Click事件?

use*_*285 15 qt

如何QLineEdit在Qt中获得Click事件?

我无法看到任何与点击相关的SLOT QLineEdit

buc*_*uck 24

我不认为继承QLineEdit是正确的选择.如果你不需要,为什么要子类?您可以改为使用事件过滤器.查看QObject :: eventFilter.

例:

MyClass::MyClass() :
    edit(new QLineEdit(this))
{
    edit->installEventFilter(this);
}

bool MyClass::eventFilter(QObject* object, QEvent* event)
{
    if(object == edit && event->type() == QEvent::FocusIn) {
        // bring up your custom edit
        return false; // lets the event continue to the edit
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 没有`QEvent :: MouseClick`事件。使用`QEvent :: MouseButtonPres`代替 (3认同)