Results 1 to 2 of 2

Thread: Faking layout of custom editor widget in item delegate

  1. #1
    Join Date
    May 2011
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Faking layout of custom editor widget in item delegate

    I have this TreeView that has a custom item delegate whose class I've inherited from QStyledItemDelegate. The view is basically used as a parameter editor where one column displays the name of the parameter, and the other column shows the current value of the parameter and can also be used to edit and and enter in a new value for the parameter.

    For the majority of the items, I simply use a QLineEdit as the item's editor in my custom delegate class. However, for other items that represent filesystem locations, I've created a custom widget that's simply a QLineEdit (displaying the path) and a QPushButton (a "Browse..." button that opens up a choose folder dialog) put together in a horizontal box layout.

    As can be seen from the attached image, I can properly create and display the editor widget whenever I'm currently editing the parameter (e.g., the job_directory parameter). But now I want to override the QStyledItemDelegate:aint() method to render the controls for my custom widget without actually having to create and use an instance of my custom widget (since, from what I understand, that's the preferred way of rendering editors within a delegate).

    My question is how do I properly calculate the location and size of each of my widget's subcontrols when I don't have the actual widget instances myself? Essentially I'd want to do whatever it is that the QHBoxLayout object is doing in my custom widget so that I can fake the layout of the subcontrols and render them using the QStyle painter, but I don't know how to do this (or if it's even possible). I've looked at the various QLayout-derived classes as well as the documentation and examples for QAbstractItemDelegate and friends, but I haven't had luck in finding out how to do proper layout of custom editors for painting purposes. If anyone has any examples or links to docs that I should look over that'll give me a better idea of how to accomplish this, I'd greatly appreciate it.

    For reference, here's my custom widget class and parts of my item delegate class:

    Qt Code:
    1. from PySide import QtCore
    2. from PySide import QtGui
    3.  
    4. class FilePathEditor( QtGui.QWidget ):
    5. def __init__( self, parent=none, window_flags=QtCore.QtWidget ):
    6. super( FilePathEditor, self ).__init__( parent, window_flags )
    7. self.path_line = QtGui.QLineEdit( self )
    8. self.browse_button = QtGui.QPushButton( "Browse...", self )
    9.  
    10. layout = QtGui.QHBoxLayout()
    11. layout.setContentsMargins( 0, 0, 0, 0 )
    12. layout.setSpacing( 1 )
    13. layout.addWidget( self.path_line )
    14. layout.addWidget( self.browse_button )
    15. self.setLayout( layout )
    16.  
    17. self.browse_button.clicked.connect( self.choose_path )
    18.  
    19. # ... additional methods removed for brevity....
    20.  
    21.  
    22. class ParameterItemDelegate( QtGui.QStyledItemDelegate ):
    23. def __init__( self, parent=None ):
    24. super( ParameterItemDelegate, self ).__init__( parent )
    25.  
    26. def updateEditorGeometry( self, editor, option, index ):
    27. editor.setGeometry( option.rect )
    28.  
    29. def paint( self, painter, option, index ):
    30. # grab pointer to my internal data structure for each item stored in my tree model.
    31. item = index.internalPointer()
    32.  
    33. if index.column() == VALUE_COLUMN and item.has_path_data():
    34. # XXX: how to properly draw my custom editor widget?
    35. else:
    36. super( ParameterItemDelegate, self ).paint( painter, option, index )
    37.  
    38. # ... additional methods removed for brevity....
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Faking layout of custom editor widget in item delegate

    Hi, what you need is QStyle::drawControl() with QStyle::CE_PushButton. Further you need to calculate the rect parameter of the QStyleOption yourself. For that use QStyle::pixelMetric() with QStyle::PM_ButtonMargin, QStyle::PM_DefaultFrameWidth, QStyle::PM_LayoutLeftMargin, ...

  3. The following user says thank you to Lykurg for this useful post:

    mvromer (12th May 2011)

Similar Threads

  1. Item Delegate - Relay a signal to editor widget
    By LynneV in forum Qt Programming
    Replies: 3
    Last Post: 5th July 2010, 16:36
  2. Item Delegate Editor Size
    By QAlex in forum Qt Programming
    Replies: 0
    Last Post: 19th April 2010, 16:43
  3. Item Delegate editor background
    By LynneV in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2010, 21:33
  4. Replies: 2
    Last Post: 12th February 2010, 04:41
  5. Replies: 5
    Last Post: 10th August 2009, 10:50

Tags for this Thread

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.