Results 1 to 6 of 6

Thread: Custom QItemDelegate: mouse hover, margins, colors?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Qt products
    Platforms
    Unix/X11

    Cool Re: Custom QItemDelegate: mouse hover, margins, colors?

    Just what I needed. Although a programmer of 20 years, I struggle very hard with the PyQt stuff. Models are difficult! Anyway many thanks for this wonderful example.

    HW

  2. #2
    Join Date
    Apr 2009
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Custom QItemDelegate: mouse hover, margins, colors?

    You're welcome, I'm glad it's useful. I am myself struggling with the Qt models and I do agree they are difficult.

    I also wanted to post an update: a slight change to the code above so that the delegate will work with (key, display text) pairs, where key is a meaningful value stored by the model (in my actual program from which this example is taken it is a column in an sql table which is the foreign key to the table where the display text is stored). In any case, the code above just stores the combobox's index values in the model, whereas the code below is slightly more sophisticated and allows for storing arbitrary values in the model, using the UserRole property of the combobox. Hope it's useful to someone out there!

    Qt Code:
    1. #! /usr/bin/env python
    2. # -*- coding: utf-8 -*-
    3. import sys
    4. from PyQt4 import QtCore
    5. from PyQt4 import QtGui
    6.  
    7. class MyRelationalDelegate(QtGui.QStyledItemDelegate):
    8. def __init__(self, parent=None):
    9. self.choices = {1: 'One', 2: 'Two', 3:'Three', 7: 'Seven'}
    10. super(MyRelationalDelegate, self).__init__(parent)
    11.  
    12. def paint(self, painter, option, index):
    13. styleOption = QtGui.QStyleOptionViewItemV4(option)
    14. # define the text to be shown
    15. text = self.choices[index.data(QtCore.Qt.DisplayRole).toInt()[0]] # value in choices dict corresponding to integer stored in tablewidget model
    16. styleOption.text = text
    17. # paint the cell
    18. self.parent().style().drawControl(QtGui.QStyle.CE_ItemViewItem, styleOption, painter)
    19.  
    20. def createEditor(self, parent, option, index):
    21. combobox = QtGui.QComboBox(parent)
    22. combobox.setEditable(True)
    23. for c in sorted(self.choices.items()):
    24. combobox.addItem(c[1], c[0]) # Add items to combobox, storing display text in DisplayRole and underlying value in UserRole
    25. return combobox
    26.  
    27. def select_line_editor(self):
    28. self.line_editor.setText("")
    29.  
    30. def setEditorData(self, editor, index):
    31. pos = editor.findData(index.data().toInt()[0])
    32. if pos == -1: #text not found, set cell value to first item in VALUES
    33. pos = 0
    34. editor.setCurrentIndex(pos)
    35.  
    36. def setModelData(self, editor, model, index):
    37. model.setData(index, QtCore.QVariant(editor.itemData(editor.currentIndex()))) # editor.currentIndex es la posición actual en el combobox; itemData recupera (por default) la data UserRole almacenada allÃ*, que en este caso es el valor del foreign key
    38.  
    39.  
    40. class myWindow(QtGui.QDialog):
    41. def __init__(self):
    42. QtGui.QDialog.__init__(self)
    43. DATA = [['First row', 1, 1], ['Second Row', 2, 2]]
    44. self.table = QtGui.QTableWidget(self)
    45. self.table.setSortingEnabled(False)
    46. self.table.setRowCount(len(DATA))
    47. self.table.setColumnCount(len(DATA[0]))
    48. self.table.setItemDelegateForColumn(1, MyRelationalDelegate(self))
    49. for row in range(len(DATA)):
    50. for col in range(len(DATA[row])):
    51. item = QtGui.QTableWidgetItem(str(DATA[row][col]))
    52. self.table.setItem(row, col, item)
    53. layout = QtGui.QVBoxLayout()
    54. layout.addWidget(self.table)
    55. self.setLayout(layout)
    56.  
    57.  
    58. if __name__ == "__main__":
    59. app = QtGui.QApplication(sys.argv)
    60. window = myWindow()
    61. window.show()
    62. app.exec_()
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 26
    Last Post: 7th January 2016, 20:26
  2. Hover on mouse over while dragging
    By mooreaa in forum Qt Programming
    Replies: 3
    Last Post: 6th February 2010, 10:31
  3. QSystemTrayIcon capture mouse hover event
    By alan in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 19:42
  4. Replies: 1
    Last Post: 7th July 2009, 16:46
  5. segmentation fault on mouse hover
    By elessaar in forum Qt Programming
    Replies: 6
    Last Post: 26th August 2008, 12:51

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
  •  
Qt is a trademark of The Qt Company.