Results 1 to 4 of 4

Thread: [PyQt] Selection dissapears when deselecting the delegate

  1. #1
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default [PyQt] Selection dissapears when deselecting the delegate

    Hi,

    I have a problem with my QComboBox delegate. The selection of the ComboBox dissapears when I deselect the delegate. I pasted the delegate code below. Did I forget to overload a QItemDelegate method ?

    I heard about an openPersistantEditor method but I don't know where to put it in my code.

    What am I missing ?

    Thanks for your help.


    Qt Code:
    1. class ComboBoxDelegate(QItemDelegate):
    2. def __init__(self, owner, itemslist):
    3. QItemDelegate.__init__(self, owner)
    4. self.itemslist = itemslist
    5.  
    6. def paint(self, painter, option, index):
    7. # Check if we're on top level of tree
    8. leftmostSibling = index.sibling(index.row(), 0)
    9. if leftmostSibling.parent().isValid():
    10. # We're not on top level of tree
    11. value = index.data(Qt.DisplayRole)
    12. print("paint" + value)
    13.  
    14. # Fill style options with item data
    15. style = QApplication.style()
    16. opt.currentText = self.itemslist.stringList()[0]
    17. opt.rect = option.rect
    18.  
    19. # Draw item data as ComboBox
    20. style.drawComplexControl(QStyle.CC_ComboBox, opt, painter)
    21.  
    22. def createEditor(self, parent, option, index):
    23. # Check if we're on top level of tree
    24. leftmostSibling = index.sibling(index.row(), 0)
    25. if leftmostSibling.parent().isValid():
    26. # We're not on top level of tree
    27. value = index.data(Qt.DisplayRole)
    28. # create the QStyleOptionComboBoxBox as our editor.
    29. editor = QComboBox(parent)
    30. editor.setModel(self.itemslist)
    31. if value == int:
    32. editor.setCurrentIndex(value)
    33. editor.installEventFilter(self)
    34. return editor
    35.  
    36. def setEditorData(self, editor, index):
    37. value = index.data(Qt.DisplayRole)
    38. editor.setCurrentIndex(value)
    39.  
    40. def setModelData(self, editor, model, index):
    41. value = editor.currentIndex()
    42. model.setData(index, QVariant(value))
    43.  
    44. def updateEditorGeometry(self, editor, option, index):
    45. editor.setGeometry(option.rect)
    46.  
    47. def sizeHint(self, option, index):
    48. # Overload sizeHint method so that the QComboBox isn't shrinked
    49. # by the QTreeView row height
    50. index_data = index.data(Qt.SizeHintRole)
    51. if index_data is None:
    52. return QSize(20, 20)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt] Selection dissapears when deselecting the delegate

    To be more specific, when I change the index of the ComboBox it shows the index label until I deselect the ComboBox. Then there is no label.

  3. #3
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt] Selection dissapears when deselecting the delegate

    OK i finally found the solution : I needed to call the drawText method in paint.

    Qt Code:
    1. def paint(self, painter, option, index):
    2. # Check if we're on top level of tree
    3. leftmostSibling = index.sibling(index.row(), 0)
    4. if leftmostSibling.parent().isValid():
    5. # We're not on top level of tree
    6. value = index.data(Qt.DisplayRole)
    7. print(value)
    8.  
    9. # Fill style options with item data
    10. style = QApplication.style()
    11. #opt.currentText = str(self.itemslist.stringList[0])
    12. opt.rect = option.rect
    13.  
    14. # Draw item data as ComboBox
    15. style.drawComplexControl(QStyle.CC_ComboBox, opt, painter)
    16. painter.drawText(option.rect, Qt.AlignVCenter, str(value))
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: [PyQt] Selection dissapears when deselecting the delegate

    Thank you!!! You saved me time.

Similar Threads

  1. Deselecting selection on QWebView
    By lukass in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2011, 13:07
  2. [PYQT] QTreeWidget item delegate problem !
    By GMib in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2010, 22:26
  3. Deselecting selection on QTableWidget
    By grantbj74 in forum Newbie
    Replies: 3
    Last Post: 27th May 2010, 05:28
  4. How to get index of a combobox delegate selection
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2009, 17:37
  5. How to draw selection in delegate
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 21:24

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.