4

我希望能够知道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);
}

并且已经像这样m_passwordLineEditLoginDialog类构造函数中注册:

m_passwordLineEdit->installEventFilter(this);

它陷入了MessageBox-es的无限循环。请帮我解决这种情况。实际上,我想用弹出窗口(而不是QMessageBox)来实现这个功能。可以满足QLabel这种需求吗?

4

3 回答 3

6

另外,请告诉我如何使用 focusInEvent() 函数来了解 QLineEdit myEdit; 对象获得焦点。

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

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

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

希望能帮助到你 !

于 2010-06-10T09:14:19.743 回答
4

像这样的东西:

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

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

于 2010-06-10T07:23:43.170 回答
0

如果您想知道何时有人点击了一个小部件,您应该覆盖mousePressEvent (QMouseEvent* event). AfocusInEvent可以由鼠标点击以外的其他来源触发。

例如:

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当然也可以这样做。

于 2010-06-10T07:25:23.743 回答