我构建了一个继承自QWidget的新元素,它是LineEdit和Button的组合。
现在,当我单击该按钮时,应该显示一个列表(或者换句话说,应该显示一个完成器)。
现在的要求是,当我在完成器之外单击鼠标时,将关闭完成器。因此,我需要知道鼠标的全局坐标,以检查鼠标是否在完成程序内。我该怎么办?
LineEditWithButton.h
class LineEditWithButton : public QWidget
{
Q_OBJECT
public:
LineEditWithButton( QWidget *p_parent = nullptr );
void showCompleter(); /* This function shows the Completer */
.............
protected:
void keyPressEvent( QKeyEvent *p_event ) override;
void mousePressEvent( QMouseEvent *p_event ) override;
.........
private slots:
void toggleCompleter(); /* slot to hide/show the completer */
void lineEditChanged(); /* slot when text in the LineEdit changes */
.............
private:
MyButton *m_button = nullptr; /* This variable holds the Button beside the LineEdit */
MyLineEdit *m_lineEdit = nullptr; /* This variable holds the LineEdit */
MyCompleter *m_completer = nullptr; /* This variable holds the Completer */
.........
};
LineEditWithButton.cpp
void LineEditWithButton::mousePressEvent( QMouseEvent *p_event )
{
QPoint globalPosition = p_event->globalPos();
QRect compRect = m_completer->geometry();
if ( !compRect.contains( globalPosition ) && m_completer->isVisible() )
{
m_completer->hide();
}
}
此功能不适用于我。我在此函数中设置了一个断点,但是没有找到该断点。我该如何解决我的问题?