如何在Qt中获得QLineEdit
的点击事件?
我无法看到与QLineEdit
点击相关的任何SLOT?
答案 0 :(得分:21)
我不认为继承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;
}
答案 1 :(得分:8)
您需要在扩展QLineEdit的新类中重新实现 focusInEvent 。以下链接将帮助您。
答案 2 :(得分:4)
虽然没有“点击”或“已进入”事件。
可以使用void cursorPositionChanged(int old, int new)
Signal。它是在用户单击lineedit(如果已启用)时以及在其他几种情况下发出的,因此您必须验证实际发生了哪些事件但我认为这仍然比子类化或使用事件侦听器对某些应用程序更容易
答案 3 :(得分:2)
如果这会有所帮助,我不知道 我必须在输入文本后调用函数。这就是我做到的。
connect(ui->passwordSetLineEdit,SIGNAL(textEdited(QString)),this,SLOT(onTextEdit(QString)));
当输入文本时,将发出textEdited信号,因此我的onTextEdit函数将被调用。
答案 4 :(得分:1)
clicked()
没有QLineEdit
之类的信号,但您可以将其子类化并在mouseReleaseEvent
的自定义实现中发出此类信号。