QCompleter and Tab Key

I try to complete the completion by clicking the tab, you get the first completion of all the features.

But in the main window based on QWidget clicking on the tab will make QLineEdit focus, after which the completion popup window ends.

Is there any way to fix this?

+4
source share
3 answers

Have you tried subclassing QLineEdit and intercepting a key press event ?

Alternatively, you can configure an event filter .

+3
source

There is probably a better solution, but a change in focus policy for all other form widgets comes to mind, which does not include focus. The only options that do not use the tab key are Qt::ClickFocus and Qt::NoFocus .

+1
source

Phew It took me a while to figure this out. Several times I tried to solve this problem, but always gave up. Now I dug enough to find the answer.

OP, please forgive me, because here is Python code, but it should be clear and work in C ++.

Basically, the problem was "how to select an entry in QCompleter"; I didn’t notice this before, but the answer is in the popup() method. QCompleter works with a model and view that contains everything you need to show.

You can change the current row as you like, then get the index of this row in the model, and then select it in the pop-up window.

In my code, I have subclassed QLineEdit , created a tabPressed signal that is emitted each time Tab is pressed. Then connect this signal to a method of the same class that does this:

  • get the current index;
  • select an index in a popup window;
  • go to the next line.

As an implementation, this is very trivial, but enough for my current purpose. Here's the skeleton (only for part of the tab, it skips 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 tweak / fix a few things, but this is the main idea.

EDIT:

See details

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

There is a small problem: when Return is pressed, everything does not work properly. Perhaps you can find a solution to this problem in the link above or in the link resources. I will fix this in the next few days and update this answer.

0
source

Source: https://habr.com/ru/post/1393485/


All Articles