PDA

View Full Version : How to edit Horizontal Header Item in QTableWidget



ioannis
3rd April 2008, 09:13
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.

patrik08
3rd April 2008, 09:39
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

ioannis
3rd April 2008, 10:03
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?

luoyes
10th April 2010, 02:57
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.

mechsin
26th April 2011, 15:15
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.

mechsin
12th July 2012, 12:42
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.


class MetaHeaderView(QtGui.QHeaderView):

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('')
self.setCurrentIndex(QtCore.QModelIndex())

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.sectionViewportPositio n(section))
self.line.setGeometry(edit_geometry)

self.line.setText(self.model().dataset.field(secti on).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

sodajo
5th March 2013, 18:50
Thank you mechsin -- this was just what I was looking for! I shared your answer also on Stack Overflow here:

http://stackoverflow.com/questions/12305894/set-pyqt-qtablewidget-horizontal-header-lable-editable/15231642#15231642