- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序使用一个 ui-form-file,它在其他小部件旁边包含一个名为“grip”(其对象名称)的标签。当我运行代码时,我看到代码行 grip was not detected
并且我想知道为什么无法识别鼠标点击标签。我还定义了一个 mousePressEvent(QMouseEvent *event)
,如果我单击该标签,它会按预期工作。
bool Note::eventFilter(QObject *target, QEvent *event)
{
if (event->type()==QEvent::MouseButtonPress){
qDebug() << "in Note::eventFilter" << endl;
if (target->objectName()=="grip")
{
lastClicked="grip";
qDebug() << "lastClicked == grip" << endl;
}
else
qDebug() << "grip was not detected" << endl;
}
return false;
}
target->objectName()=="grip"
可能是什么原因,如果我点击那个目标并且它被称为“grip”?
编辑:我的事件函数就是这样定义的:
void Note::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
qDebug() << "Left mouse button click detected";
...
主事件过滤器在 Note 的构造函数中初始化:
Note::Note(std::vector<Note *> *nListIn){
qDebug() << "in Note::Note()" << endl;
ui.setupUi(this);
installEventFilter(this);
setWindowFlags(Qt::FramelessWindowHint);
this->show(); //must be after the Qt::FramelessWindowHint
nList = nListIn;
nList->push_back(this);
qDebug() << "Size of nList (aka noteList)" << nList->size() << endl;
}
编辑 2:找到一些描述,这可能是原因吗?
If your widget only contains child widgets, you probably do not need to implement any event handlers. If you want to detect a mouse click in a child widget call the child's underMouse() function inside the widget's mousePressEvent().
最佳答案
根据定义,如果您仅在其自身上安装事件过滤器(通过调用 installEventFilter(this)
,则以下成立:
bool Note::eventFilter(QObject *target, QEvent *) {
Q_ASSERT(target == this);
...
}
显然,目标永远不会被称为 grip
,除非您以这种方式命名了 Note
类的实例。
如果你想过滤 handle 标签上的事件,那么你必须在那个标签上安装事件过滤器,而不是在 Note
小部件上。 Note
小部件只会获取 children 忽略的事件,到那时,您“过滤”它们并不重要 - 为时已晚。
您的设置代码可能包含,例如:
ui.grip->installEventFilter(this);
或者,不假设 ui
类的结构:
QWidget * grip = findChild<QWidget*>("grip");
if (grip) grip->installEventFilter(this);
关于c++ - Qt eventfilter 不检测 objectName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21789070/
这已经困扰我两天多了,所以我想我应该问一下。我在 Win7 上使用 Qt 4.5.3(用 VC2008 编译)。 我有 MyGraphicsView(继承 QGraphicsView)和 MyFilt
我可以设置我的eventFilter来忽略小部件的“默认”事件,例如。 mousePressEvents 等?或者这两者可以混合在一起吗? 在我的代码中,我有一个 mousePressEvent 和一
我的自定义标签上有一个 eventFilter,我想用它来嗅探双击。这可能吗? self.installEventFilter(self) # Handles mouse events def
我有一个小问题,我需要将我的事件过滤器设置为 QComboBox 弹出窗口。我需要在按下左右键时捕获事件。我该怎么做? 谢谢! 最佳答案 您需要在 QComboBox 的 view() (http:/
我已经用谷歌搜索了一段时间,我发现基本上,一些网页说没有大的差异。除了一些点: EventFilter 在 EventHandler 之前执行 EventFilter 对 event.Consume(
我有一个带有数百个自定义按钮的应用程序,每个按钮都需要多个信号连接。连接调用似乎非常慢,因此我尝试使用输入和离开事件通过主窗口的 eventFilter 连接/断开每个按钮的信号。 但是,有时这些事件
我的程序使用一个 ui-form-file,它在其他小部件旁边包含一个名为“grip”(其对象名称)的标签。当我运行代码时,我看到代码行 grip was not detected 并且我想知道为什么
我正尝试在 PyQT5 中实现一个事件,但出现此错误: TypeError: installEventFilter(self, QObject): argument 1 has unexpected
我的事件过滤器存在根本性错误,因为它让每个 单个事件通过,而我想停止一切。我已经阅读了很多关于 QEvent、eventFilter() 等的文档,但显然我遗漏了一些重要的东西。本质上,我正在尝试基于
在这里和其他类似 qtcentre 的地方搜索我看到这个问题已经出现但似乎无法让它工作。我有一个带有 QSplitter 的 MainWindow 小部件,它包含两个 Pane 小部件(从 QFram
我在我的 QLineEdit 上安装了一个 EventFilter,这样我就可以处理焦点事件以允许 QFileDialog 在它出现时显示获得焦点。 我添加了以下几行: QLineEdit *proj
我正在尝试创建一个 QLineEdit 元素,其文本将在单击时自动复制到剪贴板。 我创建了以下 eventFilter 来捕获点击事件并将其安装在适用的元素上: bool MainWindow::ev
当我在其中一个行编辑器中插入一个数字时,我想让一个按钮可见。按钮和 linedit 在同一行。我知道 lineedit 的位置或名称,但我不知道如何链接回按钮以使其可见或能够更改颜色。 如果您查看 e
我希望在选择 qtreeview 时按下回车键时发生“事件”。因为我不喜欢将 qtreeview 子类化(对设计者来说更容易)——我尝试安装一个事件过滤器。然而,这似乎没有用: 该类仅包含一个公共(p
我遇到了 QTextEdit 的问题。我正在为 QTextEdit 使用 eventFilters。当我在 QTextEdit 中按下小键盘 (0-9) 中的键时,它将自动完成句子这是连接那个号码。例
我是这里的 Qt 新用户。我有一个项目,我要使用 pyuic 生成的 .py 文件,但我无权访问它。 我还应该在某些对象上安装事件过滤器。是否可以在生成的 .py 文件之外使用 object.inst
它根本不起作用。我启用了鼠标跟踪,然后安装了事件过滤器,一切正常,除了 MouseMove 事件。有什么帮助吗? #include "mainwindow.h" #include "ui_mainwi
我希望能够删除使用 lambda 设置的事件过滤器。我不确定这是否可能? 我看过这个使用内部类的例子:http://www.java2s.com/Tutorials/Java/JavaFX/1120_
当我的鼠标位于labelname(QLabel)时,我希望我的程序打印“mouse on label name”,并打印“mouse not on label” name"当我的鼠标不在 labeln
我尝试创建一个可以处理放置事件的 QTableView。出于应用程序架构的原因,我希望通过我的 eventFilter 来完成(它也处理一些用于剪贴板交互的 QAction 触发器)。但是 drop-
我是一名优秀的程序员,十分优秀!