Results 1 to 4 of 4

Thread: QCompleter causes table to lose focus

  1. #1
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QCompleter causes table to lose focus

    So I've subclassed QItemDelegate to add a QCompleter to the QLineEdit widgets that are created for editing a table cell. However, as soon as I type the first letter of a word that's in the completer, the entire table loses focus and the next widget is selected. This _doesn't_ happen if the word isn't in the completer, or the cell is activated for editing.

    This seems like buggy behavior to me. I'm wondering if anyone else has come across this or can confirm this, and if anyone might be able to suggest a possible work around. Setting edit triggers to AllEditTriggers mitigates this somewhat, but isn't foolproof. Doing that also seems to make it harder to hook into events to customize the table's focus behavior (which is something else I'm trying to do).

    I'm using PyQt 4.7.3 on Debian Linux, but this appears to be the same issue that's described in this thread. Other than that, I haven't found any other resources discussing this.

    Here's a code snippet that demonstrates the problem:

    Qt Code:
    1. import sys
    2.  
    3. from PyQt4.QtCore import Qt
    4. from PyQt4.QtGui import *
    5.  
    6.  
    7. class TableDelegate(QItemDelegate):
    8.  
    9. def createEditor(self, parent, option, idx):
    10. editor = super(TableDelegate, self).createEditor(parent, option, idx)
    11.  
    12. completer = QCompleter(['fee', 'fie', 'foe', 'fum'], editor.parent())
    13. completer.setCaseSensitivity(Qt.CaseInsensitive)
    14. editor.setCompleter(completer)
    15.  
    16. return editor
    17.  
    18.  
    19. def main():
    20. app = QApplication(sys.argv)
    21.  
    22. table = QTableWidget(2, 4)
    23. table.setItemDelegate(TableDelegate())
    24.  
    25.  
    26. layout = QFormLayout()
    27. layout.addRow(table)
    28. layout.addRow(buttons)
    29.  
    30. view = QDialog()
    31. view.setLayout(layout)
    32. view.show()
    33.  
    34. buttons.accepted.connect(view.accept)
    35. buttons.rejected.connect(view.reject)
    36.  
    37. app.exec_()
    38.  
    39.  
    40. if __name__ == '__main__':
    41. main()
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated!

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QCompleter causes table to lose focus

    EDIT: Sorry, missed in your post where you said that you tried this. BTW your sample code works fine here with "AllEditTriggers" set.


    Try this:
    Qt Code:
    1. table.setEditTriggers(QAbstractItemView.AllEditTriggers)
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 28th August 2010 at 22:18.

  3. #3
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QCompleter causes table to lose focus

    Yeah, the completer works if the cell is in edit mode, but I don't really find setting AllEditTriggers a satisfactory solution.

    1. Hitting escape ends editing and keeps the cell selected. If you start typing again, and enter the first letter of a word in the completer, the table loses focus. So the problem is still present, but just being masked by the cell being put in edit mode.

    2. In my actual code, I'm using this table in a larger dialog, and am registering an event handler to allow the user to tab "through" the table (i.e., if you're on the last cell and hit tab, the next widget is selected, rather than wrapping back to the first cell). Having AllEditTriggers set makes accomplishing this quite a bit more complicated.

    If possible, I'd like to try to solve the actual problem, rather than just masking its occurrence. But it seems like this should just work. That's why I'm thinking this might be a bug in Qt.
    Last edited by knack; 29th August 2010 at 22:09.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QCompleter causes table to lose focus

    You need to override some of events to keep the focus in the widget. I don't remember the exact routine right now but you may look at QtCreator's source code for its Quick Search widget, I think it was overriding the events too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. correct event for lose focus in a QGLWidget window
    By john_god in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2009, 01:34
  2. Replies: 1
    Last Post: 12th October 2008, 08:21
  3. i want to lose the focus...
    By jrodway in forum Qt Programming
    Replies: 7
    Last Post: 14th July 2007, 10:04
  4. QCanvas automatically lose its focus.
    By Cutey in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2007, 11:29
  5. [itemView] QHeaderView lose mapping at column insertion
    By lauranger in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2006, 18:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.