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.
Re: How to edit Horizontal Header Item in QTableWidget
Quote:
Originally Posted by
ioannis
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
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?
Re: How to edit Horizontal Header Item in QTableWidget
Quote:
Originally Posted by
ioannis
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.
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.
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.
Code:
def __init__(self,orientation,parent=None):
super(MetaHeaderView, self).__init__(orientation,parent)
self.setMovable(True)
self.setClickable(True)
# This block sets up the edit line by making setting the parent
# to the Headers Viewport.
self.
line = QtGui.
QLineEdit(parent
=self.
viewport()) #Create self.line.setAlignment(QtCore.Qt.AlignTop) # Set the Alignmnet
self.line.setHidden(True) # Hide it till its needed
# This is needed because I am having a werid issue that I believe has
# to do with it losing focus after editing is done.
self.line.blockSignals(True)
self.sectionedit = 0
# Connects to double click
self.sectionDoubleClicked.connect(self.editHeader)
self.line.editingFinished.connect(self.doneEditing)
def doneEditing(self):
# This block signals needs to happen first otherwise I have lose focus
# problems again when there are no rows
self.line.blockSignals(True)
self.line.setHidden(True)
oldname = self.model().dataset.field(self.sectionedit)
newname = str(self.line.text())
self.model().dataset.changeFieldName(oldname, newname)
self.line.setText('')
def editHeader(self,section):
# This block sets up the geometry for the line edit
edit_geometry = self.line.geometry()
edit_geometry.setWidth(self.sectionSize(section))
edit_geometry.moveLeft(self.sectionViewportPosition(section))
self.line.setGeometry(edit_geometry)
self.line.setText(self.model().dataset.field(section).name)
self.line.setHidden(False) # Make it visiable
self.line.blockSignals(False) # Let it send signals
self.line.setFocus()
self.line.selectAll()
self.sectionedit = section
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