捕获 QComboBox 中的文本更改事件

发布于 2024-08-04 01:43:47 字数 182 浏览 8 评论 0原文

我正在 Red Hat Linux 上开发 Qt 应用程序。我想在 QComboBox 中捕获回车键按下事件。

我已将一个插槽连接到信号 editTextChanged(),该信号会在每次按键时发出,但不会在 Enter 键 时发出。
为什么会这样呢?还有其他方法可以检测回车符吗?

I am developing a Qt application on Red Hat Linux. I want to capture Carriage Return key press events in a QComboBox.

I have connected a slot to the signal editTextChanged() which is emitted for every key press but not for the Enter Key.
Why so? Is there any other way to detect Carriage Returns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-08-11 01:43:47

我假设您编写了一个插槽并将其连接到 QComboBox::editTextChanged() 信号。
当文本更改并且 Enter 不更改文本而是接受文本时,会触发此信号。如果您想捕获回车符,可以采用多种方法。

  1. 子类QComboBox
    覆盖 keyPressEvent() :首先调用 QComboBox::keyPressEvent() ,然后检查按下的键是否为 Enter。如果是,则发出信号。
    当你需要的时候就可以使用这个子类。如果您不知道如何,请搜索有关在 QDesigner 中推广小部件的信息。

  2. 实现一个继承QObject的新类。在此类中,重写 eventFilter() :检查事件是否为按键事件。如果是,请检查它是否是 Enter 键。如果是,则发出信号。
    然后,创建此类的一个实例并将其设置为 QComboBox 的事件过滤器。将插槽连接到您实现的该实例的信号。

如果这些不清楚,我建议阅读以下页面:

使用自定义小部件使用 Qt 设计器

Qt 事件和过滤器事件过滤器

I am assuming you wrote a slot and connected it to QComboBox::editTextChanged() signal.
This signal is fired when the text changes and Enter does not change the text, it accepts it. If you want to capture Carriage Return, there are a number of ways you can follow.

  1. Subclass QComboBox.
    Override keyPressEvent() : first call QComboBox::keyPressEvent() and then check if the pressed key is Enter. If it is, emit a signal.
    Use this subclass whenever you need. Search about promoting widgets in QDesigner if you don't know how.

  2. Implement a new class which inherits QObject. In this class, override eventFilter() : check if the event is a key press. If it is, check if it is the Enter key. If it is, emit a signal.
    Then, create an instance of this class and set it as event filter to your QComboBox. Connect a slot to this instance's signal, which you implemented.

If these are not clear, i recommend reading the following pages:

Using Custom Widgets with Qt designer

Qt Events & Event Filters

千仐 2024-08-11 01:43:47

您还可以查看 activated( const QString& ) 信号。当用户按回车键时可能会发出它。

You could also look into the activated( const QString& ) signal. It might be emitted when the user hits enter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文