如何知道 QLineEdit 是否获得焦点?

发布于 2024-09-04 20:42:28 字数 1026 浏览 11 评论 0原文

我希望能够知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

或十年 2024-09-11 20:42:28

另外,请告诉我如何使用
focusInEvent() 函数以便
知道 QLineEdit myEdit;对象得到
重点。

您应该将自己连接到以下信号:

void QApplication::focusChanged ( QWidget * old, QWidget * now )   [signal]

当新的 QWidget 是您的 QLineEdit 时,您知道它获得了焦点!

希望有帮助!

Also, please tell me how to use
focusInEvent() function in order to
know if QLineEdit myEdit; object got
focus.

You should connect yourself to the following SIGNAL :

void QApplication::focusChanged ( QWidget * old, QWidget * now )   [signal]

When the new QWidget is your QLineEdit, you know it got focus !

Hope it helps !

も星光 2024-09-11 20:42:28

类似这样的内容:

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);
}

您可以找到所有可能原因的列表 在此页面

Something like that:

class YourWidget : public QLineEdit
{
    Q_OBJECT

    protected:

    void focusInEvent(QFocusEvent* e);
};

In the .cpp file:

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.

别低头,皇冠会掉 2024-09-11 20:42:28

如果您想知道何时有人单击小部件,您应该重写mousePressEvent (QMouseEvent* event)focusInEvent 可以由鼠标单击以外的其他源触发。

例如:

class MyLineEdit : public QLineEdit
{
        Q_OBJECT
    public:
        //...
    protected:
         void mousePressEvent(QMouseEvent* event)
         {
              //pass the event to QLineEdit
              QLineEdit::mousePressEvent(event);
              //register the click or act on it
         }
};

如果您确实想知道您的小部件何时获得焦点,当然可以使用 focusInEvent 执行相同的操作。

If you want to know when someone clicks in a widget, you should override mousePressEvent (QMouseEvent* event). A focusInEvent can be triggered by other sources than a mouse click.

For example:

class MyLineEdit : public QLineEdit
{
        Q_OBJECT
    public:
        //...
    protected:
         void mousePressEvent(QMouseEvent* event)
         {
              //pass the event to QLineEdit
              QLineEdit::mousePressEvent(event);
              //register the click or act on it
         }
};

If you do want to know when your widget receives focus, do the same with a focusInEvent of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文