QCompleter 和 Tab 键

发布于 2024-12-29 18:09:16 字数 152 浏览 1 评论 0原文

我试图在按 Tab 时完成一个完成,您将获得所有可能性的第一个完成。

但是,在基于 QWidget 的主窗口中,按 Tab 将使 QLineEdit 失去焦点,并且完成弹出窗口会在之后隐藏。

有办法解决吗?

I'm trying to make a Completion when pressing tab, you get the first completion of all possibilities.

But, in a QWidget-based main window, pressing tab will make that QLineEdit lost focus, and completion popup hides after that.

Is there a way to fix it ?

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

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

发布评论

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

评论(3

゛时过境迁 2025-01-05 18:09:16

您是否尝试过对 QLineEdit 进行子类化并拦截键 新闻发布会

或者,您可以设置事件过滤器

Have you tried to subclass QLineEdit and intercept the key press event?

Alternatively you could set up an event filter.

超可爱的懒熊 2025-01-05 18:09:16

呼。我花了一些时间才弄清楚这个问题:)我多次尝试解决这个问题,但总是放弃。现在,我已经挖够了,找到了答案。

OP,请原谅我,因为这里的代码是Python,但应该是可以理解的并且也适用于C++。

基本上,我遇到的问题是“如何在 QCompleter 中选择一个条目”;我之前没有注意到,但答案就在 popup() 方法中。 QCompleter 使用模型和视图,其中包含要显示的内容。

您可以根据需要更改当前行,然后获取该行在模型中的索引,然后在弹出窗口中选择它。

在我的代码中,我对 QLineEdit 进行了子类化,创建了一个 tabPressed 信号,每次按下 Tab 时都会发出该信号。然后,将此信号连接到同一类的方法,该方法执行以下操作:

  1. 获取当前索引;
  2. 在弹出窗口中选择索引;
  3. 前进到下一行。

作为实现,这是非常微不足道的,但对于我当前的目的来说这已经足够了。这是骨架(仅针对选项卡部分,它缺少模型和其他所有内容)。

class MyLineEdit(QLineEdit):
    tabPressed = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self._compl = QCompleter()
        self.tabPressed.connect(self.next_completion)

    def next_completion(self):
        index = self._compl.currentIndex()
        self._compl.popup().setCurrentIndex(index)
        start = self._compl.currentRow()
        if not self._compl.setCurrentRow(start + 1):
            self._compl.setCurrentRow(0)

    def event(self, event):
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
            self.tabPressed.emit()
            return True
        return super().event(event)

您可能需要调整/修复一些事情,但这是基本想法。

编辑:

有关详细信息,请参阅

http://www. qtcentre.org/threads/23518-How-to-change-completion-rule-of-QCompleter

有一个小问题:当按下 Return 时,事情不起作用 适当地。也许您可以在上面的链接或其中引用的资源中找到此问题的解决方案。我将在接下来的几天内修复此问题并更新此答案。

Whew. It took me some time to figure this out :) Multiple times I have tried to solve this problem, but always gave up. Now, I dug enough to find the answer.

OP, please pardon me, because the code here is Python, but should be understandable and work for C++ as well.

Basically, the problem I had was "how to select an entry in the QCompleter"; I didn't notice before, but the answer is in the popup() method. QCompleter works with a model and a view, which contains the things to show.

You can change the current row as you wish, then get the index of that row in the model, then select it in the pop-up.

In my code, I subclassed QLineEdit, created a tabPressed signal which is emitted every time Tab is pressed. Then, connected this signal to a method of the same class which does this:

  1. get the current index;
  2. select the index in the popup;
  3. advance to the next row.

As implementation, this is very trivial, but for my current purpose this is enough. Here's the skeleton (just for the tab part, it's missing the model and everything else).

class MyLineEdit(QLineEdit):
    tabPressed = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self._compl = QCompleter()
        self.tabPressed.connect(self.next_completion)

    def next_completion(self):
        index = self._compl.currentIndex()
        self._compl.popup().setCurrentIndex(index)
        start = self._compl.currentRow()
        if not self._compl.setCurrentRow(start + 1):
            self._compl.setCurrentRow(0)

    def event(self, event):
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
            self.tabPressed.emit()
            return True
        return super().event(event)

You may need to adjust/fix few things, but this is the basic idea.

EDIT:

For details see

http://www.qtcentre.org/threads/23518-How-to-change-completion-rule-of-QCompleter

There's a little issue: when Return is pressed, the things don't work properly. Maybe you can find a solution to this problem in the link above, or in the referenced resources therein. I'll fix this in the next few days and update this answer.

不可一世的女人 2025-01-05 18:09:16

可能有更好的解决方案,但我想到的一个解决方案是将表单上所有其他小部件的焦点策略更改为包含“选项卡”焦点的策略。唯一不使用 Tab 键的选项是 Qt::ClickFocusQt::NoFocus

There is probably a better solution but one that comes to mind is to change the focus policy for all other widgets on the form to something that doesn't include "tab" focus. The only options that don't use the tab key are Qt::ClickFocus and Qt::NoFocus.

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