Results 1 to 7 of 7

Thread: How to edit Horizontal Header Item in QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    3

    Default Re: How to edit Horizontal Header Item in QTableWidget

    Quote Originally Posted by ioannis View Post
    Thanks for your answer. However, what I wanted to achieve was to implement this functionality for the header items and not substitute the header items with simple items that simple look-like header items. Is there any oter way to actually edit the horizontal heaer items?
    Did you find any method to edit the header items?
    I want to insert a comboBox to the header item,but it seems that it's impossible.

  2. #2
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to edit Horizontal Header Item in QTableWidget

    I am interested also if anyone has figured out how to do this. I am using PyQt4. I have tried various things such as setting the header item to a QLineEdit object but have not gotten anything to work. I understand how patrik's solution would work but it seems to me that you should be able to make the Headers editable directly.

  3. #3
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to edit Horizontal Header Item in QTableWidget

    I found a solution to this at least in PyQt I feel its kind of a hack but that is only because the headerview can't use delegates. I also posted this on another thread but I don't know how to link them.

    Qt Code:
    1. class MetaHeaderView(QtGui.QHeaderView):
    2.  
    3. def __init__(self,orientation,parent=None):
    4. super(MetaHeaderView, self).__init__(orientation,parent)
    5. self.setMovable(True)
    6. self.setClickable(True)
    7. # This block sets up the edit line by making setting the parent
    8. # to the Headers Viewport.
    9. self.line = QtGui.QLineEdit(parent=self.viewport()) #Create
    10. self.line.setAlignment(QtCore.Qt.AlignTop) # Set the Alignmnet
    11. self.line.setHidden(True) # Hide it till its needed
    12. # This is needed because I am having a werid issue that I believe has
    13. # to do with it losing focus after editing is done.
    14. self.line.blockSignals(True)
    15. self.sectionedit = 0
    16. # Connects to double click
    17. self.sectionDoubleClicked.connect(self.editHeader)
    18. self.line.editingFinished.connect(self.doneEditing)
    19.  
    20. def doneEditing(self):
    21. # This block signals needs to happen first otherwise I have lose focus
    22. # problems again when there are no rows
    23. self.line.blockSignals(True)
    24. self.line.setHidden(True)
    25. oldname = self.model().dataset.field(self.sectionedit)
    26. newname = str(self.line.text())
    27. self.model().dataset.changeFieldName(oldname, newname)
    28. self.line.setText('')
    29. self.setCurrentIndex(QtCore.QModelIndex())
    30.  
    31. def editHeader(self,section):
    32. # This block sets up the geometry for the line edit
    33. edit_geometry = self.line.geometry()
    34. edit_geometry.setWidth(self.sectionSize(section))
    35. edit_geometry.moveLeft(self.sectionViewportPosition(section))
    36. self.line.setGeometry(edit_geometry)
    37.  
    38. self.line.setText(self.model().dataset.field(section).name)
    39. self.line.setHidden(False) # Make it visiable
    40. self.line.blockSignals(False) # Let it send signals
    41. self.line.setFocus()
    42. self.line.selectAll()
    43. self.sectionedit = section
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to mechsin for this useful post:

    sodajo (5th March 2013)

  5. #4
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: How to edit Horizontal Header Item in QTableWidget

    Thank you mechsin -- this was just what I was looking for! I shared your answer also on Stack Overflow here:

    http://stackoverflow.com/questions/1...31642#15231642

Similar Threads

  1. QTableWidget NW corner header item?
    By McKee in forum Qt Programming
    Replies: 9
    Last Post: 30th May 2012, 23:44
  2. QTableWidget item checkable and combo?
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 07:12

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.