Qt 中默认控件的 C++ 自定义事件

C++ Custom event for default controls in Qt

是否可以编写自定义事件反应并将其添加到 QLineEdit 等默认控件中。我有自己的 QSplashScreen,其中很少有控件,例如 QLabel、QLineEdit 或 QPushButton。对于其中之一,我需要为 mousePressEvent 自定义反应。怎么做?我不想创建继承 QLineEdit 的新 MyQLineEdit,我只想覆盖默认事件函数。

您可以为 QLineEdit 安装事件过滤器。有关详细信息,请参阅 Event Filters

您可以使用 installEventFilter() + 实现 eventFilter(),请参阅 Qt 文档中的 the sample。但不是 MainWindow 而是你自定义的 SplashScreen。