设置 pyqt qtablewidget 水平标题标签可编辑 [英] Set pyqt qtablewidget horizontal header lable editable
问题描述
如何通过双击来编辑 qtablewidget 中水平标题的标签?到目前为止我所拥有的但不起作用.因此,当有人双击顶部标题时,会弹出一个 linedit 允许文本,然后重置标签.
How can I edit the label of a horizontal header in a qtablewidget by double clicking on it? What I have so far but does not work. So when someone doubleclicks the top header, a linedit pops up allows for text and then resets the label.
import sys
from PyQt4 import QtGui,QtCore
import generate_file
class WebGetMain(QtGui.QMainWindow,generate_file):
def __init__(self,parent=None):
super(WebGetMain,self).__init__(parent)
self.setupUi(self)
#set these values customizable before user populates the table
self.row_count = 20
self.col_count = 20
#setup actual cols,rows
self.web_get_table.setRowCount(self.row_count)
self.web_get_table.setColumnCount(self.col_count)
#allow horizontal header to be altered
#create and initialize linedit
self.linedit = QtGui.QLineEdit(parent=self.web_get_table.viewport())
self.linedit.setAlignment(QtCore.Qt.AlignTop)
self.linedit.setHidden(True)
self.linedit.editingFinished.connect(self.doneEditing)
#connect double click action on header
self.web_get_table.connect(self.web_get_table.horizontalHeader(),QtCore.SIGNAL('sectionDoubleClicked(int)'),self.on_header_doubleClicked)
#setup vertical scrollbars for adding rows
self.vBar = self.web_get_table.verticalScrollBar()
self._vBar_lastVal = self.vBar.value()
#initialize cols,rows
for column in range(0, 2):
for row in range(0, 3):
print row, column
item = QtGui.QTableWidgetItem()
self.web_get_table.setItem(row, column, item)
#scrollbar value signal to detect for scrolling
self.vBar.valueChanged.connect(self.scrollbarChanged)
def scrollbarChanged(self, val):
#initialize scrollbar
bar = self.vBar
minVal, maxVal = bar.minimum(), bar.maximum()
avg = (minVal+maxVal)/2
rowCount = self.web_get_table.rowCount()
# scrolling down
if val > self._vBar_lastVal and val >= avg:
self.web_get_table.insertRow(rowCount)
# scrolling up
elif val < self._vBar_lastVal:
lastRow = rowCount-20
empty = True
for col in xrange(self.web_get_table.columnCount()):
item = self.web_get_table.item(lastRow, col)
if item and item.text():
empty=False
break
if empty:
#remove rows when scrolling up
self.web_get_table.removeRow(lastRow)
self._vBar_lastVal = val
def doneEditing(self):
self.linedit.blockSignals(True)
self.linedit.setHidden(True)
oldname = self.web_get_table.model().dataset.field(self.sectionedit)
newname = str(self.linedit.text())
self.web_get_table.model().dataset.changeFieldName(oldname,newname)
self.linedit.setText('')
self.web_get_table.setCurrentIndex(QtCore.QModelIndex())
def on_header_doubleClicked(self,item):
self.linedit.setText(self.web_get_table.model().field(item).name)
self.linedit.setHidden(False)
self.linedit.blockSignals(False)
self.linedit.setFocus()
self.linedit.selectAll()
self.sectionedit = item
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle(QtGui.QStyleFactory.create("Plastique"))
main_window = WebGetMain()
main_window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
推荐答案
老实说,我不明白你想用那个 QLineEdit
做什么.但据我所知,你已经成功了一半.使用 horizontalHeader()
的 sectionDoubleClicked
信号是一个好的开始.但其余的对我来说是一个很大的问号.
To be honest, I couldn't figure out what you're trying to do with that QLineEdit
. But as far as I can see, you are halfway there. Using sectionDoubleClicked
signal of the horizontalHeader()
is a good start. But the rest is a big question mark for me.
您需要做的就是:使用 horizontalHeaderItem(index)
获取标题项并使用 text
获取值或 setText
设置新值.
All you need to do is this: Get the header item with horizontalHeaderItem(index)
and use text
to get the value or setText
to set the new value.
你可能会考虑 QInputDialog.getText
从用户那里获取新值.
And you might consider QInputDialog.getText
to obtain the new value from the user.
这是一个显示这一点的最小示例:
Here is a minimal example that shows this:
import sys
from PyQt4 import QtGui
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.table = QtGui.QTableWidget(5,5)
self.table.setHorizontalHeaderLabels(['1', '2', '3', '4', '5'])
self.table.setVerticalHeaderLabels(['1', '2', '3', '4', '5'])
self.table.horizontalHeader().sectionDoubleClicked.connect(self.changeHorizontalHeader)
layout = QtGui.QHBoxLayout()
layout.addWidget(self.table)
self.setLayout(layout)
def changeHorizontalHeader(self, index):
oldHeader = self.table.horizontalHeaderItem(index).text()
newHeader, ok = QtGui.QInputDialog.getText(self,
'Change header label for column %d' % index,
'Header:',
QtGui.QLineEdit.Normal,
oldHeader)
if ok:
self.table.horizontalHeaderItem(index).setText(newHeader)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = MyWindow()
main.show()
sys.exit(app.exec_())
这篇关于设置 pyqt qtablewidget 水平标题标签可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!