如何知道 QLineEdit 是否获得焦点?
我希望能够知道在 QLineEdit
中是否有一次点击。所以我想我应该重新实现以下功能(??):
void QLineEdit::focusInEvent ( QFocusEvent * e ) [virtual protected]
我应该怎么做?
另外,请告诉我如何使用 focusInEvent()
函数来了解 QLineEdit myEdit;
对象是否获得焦点。
编辑:我编写了以下函数:
bool LoginDialog::eventFilter(QObject *target, QEvent *event)
{
if (target == m_passwordLineEdit)
{
if (event->type() == QEvent::FocusIn)
{
if(checkCapsLock())
{
QMessageBox::about(this,"Caps Lock", "Your caps lock is ON!");
}
return true;
}
}
return QDialog::eventFilter(target, event);
}
并在 LoginDialog
类构造函数中注册了 m_passwordLineEdit
,如下所示:
m_passwordLineEdit->installEventFilter(this);
它陷入了 MessageBox 的无限循环-es。请帮助我解决这种情况。实际上我想用弹出窗口(而不是用QMessageBox
)来实现这个功能。可以使用 QLabel
来满足该需求吗?
I want to be able to know if in the QLineEdit
it was a click. So I guess I should reimplement the following function(??):
void QLineEdit::focusInEvent ( QFocusEvent * e ) [virtual protected]
How should I do that?
Also, please tell me how to use focusInEvent()
function in order to know if QLineEdit myEdit;
object got focus.
EDIT: I have written the following function:
bool LoginDialog::eventFilter(QObject *target, QEvent *event)
{
if (target == m_passwordLineEdit)
{
if (event->type() == QEvent::FocusIn)
{
if(checkCapsLock())
{
QMessageBox::about(this,"Caps Lock", "Your caps lock is ON!");
}
return true;
}
}
return QDialog::eventFilter(target, event);
}
And have registered m_passwordLineEdit
in LoginDialog
class constructor like this:
m_passwordLineEdit->installEventFilter(this);
And it's falling into an infinite loop of MessageBox-es. Please help me to resolve this situation. Actually I would like to implemet this function with a pop-up window (not with a QMessageBox
). Is it OK to use QLabel
for that need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将自己连接到以下信号:
当新的 QWidget 是您的 QLineEdit 时,您知道它获得了焦点!
希望有帮助!
You should connect yourself to the following SIGNAL :
When the new QWidget is your QLineEdit, you know it got focus !
Hope it helps !
类似这样的内容:
在
.cpp
文件中:您可以找到所有可能原因的列表 在此页面。
Something like that:
In the
.cpp
file:You can find the list of all possible reasons on this page.
如果您想知道何时有人单击小部件,您应该重写
mousePressEvent (QMouseEvent* event)
。focusInEvent
可以由鼠标单击以外的其他源触发。例如:
如果您确实想知道您的小部件何时获得焦点,当然可以使用
focusInEvent
执行相同的操作。If you want to know when someone clicks in a widget, you should override
mousePressEvent (QMouseEvent* event)
. AfocusInEvent
can be triggered by other sources than a mouse click.For example:
If you do want to know when your widget receives focus, do the same with a
focusInEvent
of course.