如何知道一个QLineEdit是否有焦点? [英] How to know if a QLineEdit got focus?
问题描述
我想知道在 QLineEdit
是否是一个点击。所以我想我应该重新实现以下函数(??):
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]
我应该怎么办?
另外,请告诉我如何使用 focusInEvent()
函数,以了解 QLineEdit myEdit;
Also, please tell me how to use focusInEvent()
function in order to know if QLineEdit myEdit;
object got focus.
EDIT:我写了以下函数:
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);
}
并已注册 m_passwordLineEdit
in LoginDialog
类构造函数,如下所示:
And have registered m_passwordLineEdit
in LoginDialog
class constructor like this:
m_passwordLineEdit->installEventFilter(this);
它陷入了无限循环的MessageBox-es。请帮我解决这个情况。实际上,我想用一个弹出窗口(不是用 QMessageBox
)来实现这个函数。是否可以使用 QLabel
?
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?
推荐答案
:
class YourWidget : public QLineEdit
{
Q_OBJECT
protected:
void focusInEvent(QFocusEvent* e);
};
在 .cpp
文件中:
void YourWidget::focusInEvent(QFocusEvent* e)
{
if (e->reason() == Qt::MouseFocusReason)
{
// The mouse trigerred the event
}
// You might also call the parent method.
QLineEdit::focusInEvent(e);
}
You can find the list of all possible reasons on this page.
这篇关于如何知道一个QLineEdit是否有焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!