Results 1 to 6 of 6

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

Threaded View

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

    Default Custom QItemDelegate: mouse hover, margins, colors?

    Hello,

    I am playing around with a custom delegate for a QTableWidget, which just shows a combobox as an editor, stores the value as a number (which is a foreign key to a list), and displays the text corresponding to the item at that value's position in a list. For example: it stores 1 and displays 'one' if the list is ['someitem', 'one'], and stores 4 and displays 'cow' if the list is ['bird', 'horse', 'pig', 'snake', 'cow'].

    My problem is I cannot get the appearance of the cells which use the custom delegate 'right' - which in this case would be the same as the default delegate. In particular:
    • Hovering normal cells with the mouse produces a shadow over the hovered cell. How can I reproduce this effect in my custom delegate? I have tried to find a custom delegate with a working "hover" effect in the examples, without luck. Both the star delegate and the spin box delegate examples have the same problem in the columns affected.
    • My delegate's painter does not draw using the same cell margins as the default delegate's painter. How should I set these margins in my custom delegate?
    • Which is the correct way of looking up colors and effects to be used by the custom delegate's paint method so that it mimics the default delegate's style? In particular, the selected item under the default delegate has a sublte "glow" texture in its background,which is not reproduced by my delegate's paint method, which simply uses a solid color as background. I couldn't find this effect in the examples, either, they too seem to use a solid color as background when using custom delegates. (Edited to clarify)

    Thanks!
    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 QRelationalDelegate(QtGui.QStyledItemDelegate):
    8. VALUES = ['zero', 'one', 'two', 'three', 'four']
    9.  
    10. def paint(self, painter, option, index):
    11. value = index.data(QtCore.Qt.DisplayRole).toInt()[0] # integer stored in tablewidget model
    12. text = self.VALUES[value] # text to be displayed
    13. painter.save()
    14. if option.state & QtGui.QStyle.State_Selected: # highligh background if selected
    15. painter.fillRect(option.rect, option.palette.highlight())
    16. painter.drawText(option.rect, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, text)
    17. painter.restore()
    18.  
    19. def createEditor(self, parent, option, index):
    20. combobox = QtGui.QComboBox(parent)
    21. combobox.addItems(self.VALUES)
    22. combobox.setEditable(True)
    23. return combobox
    24.  
    25. def setEditorData(self, editor, index):
    26. text = self.VALUES[index.data(QtCore.Qt.DisplayRole).toInt()[0]]
    27. pos = editor.findText(text)
    28. if pos == -1: #text not found, set cell value to first item in VALUES
    29. pos = 0
    30. editor.setCurrentIndex(pos)
    31.  
    32. def setModelData(self, editor, model, index):
    33. model.setData(index, QtCore.QVariant(editor.currentIndex()))
    34.  
    35.  
    36. class myWindow(QtGui.QDialog):
    37. def __init__(self):
    38. QtGui.QDialog.__init__(self)
    39. DATA = [['First row', 1, 1], ['Second Row', 2, 2]]
    40. self.table = QtGui.QTableWidget(self)
    41. self.table.setSortingEnabled(False)
    42. self.table.setRowCount(len(DATA))
    43. self.table.setColumnCount(len(DATA[0]))
    44. self.table.setItemDelegateForColumn(1, QRelationalDelegate(self))
    45. for row in range(len(DATA)):
    46. for col in range(len(DATA[row])):
    47. item = QtGui.QTableWidgetItem(str(DATA[row][col]))
    48. self.table.setItem(row, col, item)
    49. layout = QtGui.QVBoxLayout()
    50. layout.addWidget(self.table)
    51. self.setLayout(layout)
    52.  
    53.  
    54. if __name__ == "__main__":
    55. app = QtGui.QApplication(sys.argv)
    56. window = myWindow()
    57. window.show()
    58. app.exec_()
    To copy to clipboard, switch view to plain text mode 
    Attached is a picture the hover effect which works for columns 0 and 2 in my example but not for column 1 which uses the custom delegate.default_hover_ef&#1.png
    Last edited by fede; 26th May 2010 at 02:32. Reason: removed useless debug leftovers from code, again

  2. The following user says thank you to fede for this useful post:

    sami1592 (2nd March 2017)

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.