设置pyqt qtablewidget水平标题标签可编辑

时间:2012-09-06 18:06:44

标签: python pyqt

如何通过双击来编辑qtablewidget中水平标题的标签? 到目前为止,我有什么,但不起作用。因此,当有人双击顶部标题时,弹出的行标题允许文本,然后重置标签。

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()

2 个答案:

答案 0 :(得分:7)

说实话,我无法弄清楚你要用QLineEdit做什么。但就我所知,你已到了一半。使用sectionDoubleClicked的{​​{1}}信号是一个好的开始。但其余的对我来说是一个很大的问号。

您需要做的就是:使用horizontalHeader()获取标题项,并使用horizontalHeaderItem(index)获取值,或text设置新值。

您可以考虑QInputDialog.getText从用户那里获取新值。

以下是一个显示此内容的最小示例:

setText

答案 1 :(得分:1)

我在metsin上找到了关于QtCentre的答案,这对我有用:

http://www.qtcentre.org/threads/12835-How-to-edit-Horizontal-Header-Item-in-QTableWidget

查看帖子中的最后评论。总之,它在表头的视口中创建了一个QLineEdit。用于双击标题的插槽显示正确位置和宽度的行编辑以覆盖标题部分。用于完成行编辑的插槽隐藏行编辑并捕获其标题项的文本值。