Results 1 to 7 of 7

Thread: How to edit Horizontal Header Item in QTableWidget

  1. #1
    Join Date
    Apr 2008
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to edit Horizontal Header Item in QTableWidget

    Is there a way to edit the horizontal header items in a QTableWidget by double-clicking the corresponding header? I have tried to connect the signal itemDoubleClicked to the header item, that I have retrieved using the horizontalHeaderItem method, and then use the editItem method for enabling editing of the header. I have also set the flags of the header items in order to be editable, but it seems that the header is not emitting any signal. Any suggestions?

    Thanks in advance.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to edit Horizontal Header Item in QTableWidget

    Quote Originally Posted by ioannis View Post
    Is there a way to edit the horizontal header items in a QTableWidget by double-clicking the corresponding header? I have tried to connect the signal itemDoubleClicked to the header item, that I have retrieved using the horizontalHeaderItem method, and then use the editItem method for enabling editing of the header. I have also set the flags of the header items in order to be editable, but it seems that the header is not emitting any signal. Any suggestions?

    Thanks in advance.
    1, Hide the header horizontalHeaderItem
    2, Display horizontalHeaderItem one first line 0
    3, make background from line 0 like horizontalHeaderItem grey..
    item->setData (Qt::BackgroundColorRole ,QColor("#grey"));

    4, insert flag editable item->setFlags ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | editable );


    all other line row go + 1

  3. #3
    Join Date
    Apr 2008
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to edit Horizontal Header Item in QTableWidget

    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?

  4. #4
    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.

  5. #5
    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.

  6. #6
    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 

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

    sodajo (5th March 2013)

  8. #7
    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: 31st May 2012, 00:44
  2. QTableWidget item checkable and combo?
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 08: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.