[英]install EventFilter on QWidget (qt4.4.3/kde4)
I have a K* window, and within it, a widget which needs the events filtered. 我有一个K *窗口,在其中,需要过滤事件的窗口小部件。
For example I do not want the possibility of clicking it... 例如,我不想点击它的可能性......
How can I do that? 我怎样才能做到这一点?
Have I to use eventfilters? 我有没有使用eventfilters? In this case, what's the best way? 在这种情况下,最好的方法是什么?
but my problem is that I can't subclass my widget,because it's a TerminalInterface->widget(), not an object like others :\\ 但我的问题是我无法子类化我的小部件,因为它是一个TerminalInterface-> widget(),而不是像其他人一样的对象:\\
Besides the setEnabled sledgehammer approach in the first answer, there are two other approaches, one of which is to use eventfilters. 除了第一个答案中的setEnabled大锤方法外,还有另外两种方法,其中一种方法是使用eventfilters。
The other is to subclass the widget, and then reimplement, say, the mouse* events. 另一种是子窗口小部件,然后重新实现鼠标*事件。 Simply leaving them empty will prevent any mouse interaction. 简单地将它们留空将阻止任何鼠标交互。 So: 所以:
MyWidget : public QSomeWidget { Q_OBJECT public: MyWidget(QWidget *parent); MyWidget:public QSomeWidget {Q_OBJECT public:MyWidget(QWidget * parent);
protected: void mousePressEvent(QMouseEvent *) {} .. etc .. }; protected:void mousePressEvent(QMouseEvent *){} .. etc ..};
QWidget has an enabled
property. QWidget有一个enabled
属性。 Just call widget->setEnabled(false)
and this will prevent it from accepting mouse clicks. 只需调用widget->setEnabled(false)
,这将阻止它接受鼠标点击。 It may also modify its appearance: for example a QPushButton
will be grayed out. 它也可能会修改它的外观:例如, QPushButton
将变灰。
Event Filters sound like overkill for what you want. 事件过滤器听起来像是你想要的过度杀戮。
It looks like eventFilter()
is what you want. 看起来像eventFilter()
就是你想要的。
Here's the section of Qt docs that talk about it: Event Filters 以下是讨论它的Qt文档部分: 事件过滤器
Basically you have to create a class that inherits QObject
and then implement the virtual function eventFilter()
. 基本上,您必须创建一个继承QObject
的类,然后实现虚函数eventFilter()
。 Then call the installEventFilter() method on the object that you want to filter with the filter as a parameter. 然后使用过滤器作为参数调用要过滤的对象上的installEventFilter()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.