复制pyqt表选择,包括列和行标题

时间:2014-07-26 13:06:47

标签: python pyqt

我想要复制qt表的选定部分,包括标题。默认的qt表副本只复制一个单元格,而我需要整个选择,可以是多列和多行。我从一些地方拼接了一些代码(大多数是here),它似乎运行良好,只是它只复制单元格(数据),而不是标题。我还需要一些帮助来获取选择的标题。这是我的瘦身样本:

from PyQt4 import QtGui, QtCore
import sys

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)

        self.table = QtGui.QTableWidget(parent=self)
        self.table.setColumnCount(2)
        self.table.setRowCount(2)
        self.table.setHorizontalHeaderLabels(['col1','col2'])
        self.table.setVerticalHeaderLabels(['row1','row2'])
        self.table.setItem(0,0,QtGui.QTableWidgetItem('foo'))
        self.table.setItem(0,1,QtGui.QTableWidgetItem('bar'))
        self.table.setItem(1,0,QtGui.QTableWidgetItem('baz'))
        self.table.setItem(1,1,QtGui.QTableWidgetItem('qux'))

        layout = QtGui.QGridLayout()
        layout.addWidget(self.table, 1, 0)
        self.setLayout(layout)

        self.clip = QtGui.QApplication.clipboard()


    def keyPressEvent(self, e):
        if (e.modifiers() & QtCore.Qt.ControlModifier):
            selected = self.table.selectedRanges()

            if e.key() == QtCore.Qt.Key_C: #copy
                s = ""

                for r in xrange(selected[0].topRow(), selected[0].bottomRow()+1):
                    for c in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1):
                        try:
                            s += str(self.table.item(r,c).text()) + "\t"
                        except AttributeError:
                            s += "\t"
                    s = s[:-1] + "\n" #eliminate last '\t'
                self.clip.setText(s)


if __name__ == '__main__':

    # Initialize the application
    app = QtGui.QApplication(sys.argv)
    mw = MainWidget()
    mw.show()
    app.exec_()

示例表如下所示:

     col1 col2
row1 foo  bar
row2 baz  qux

例如,如果我选择barbux,我希望副本为:

\t col2\n
row1 \t bar\n
row2 \t qux\n

我的实际案例有很多列和行。我的困难在于找出所选单元格的标题;我可以处理格式化。任何帮助或建议非常感谢。提前谢谢!

1 个答案:

答案 0 :(得分:5)

线索在标题项中:

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)

        self.table = QtGui.QTableWidget(parent=self)
        self.table.setColumnCount(2)
        self.table.setRowCount(2)
        self.table.setHorizontalHeaderLabels(['col1','col2'])
        self.table.setVerticalHeaderLabels(['row1','row2'])
        self.table.setItem(0,0,QtGui.QTableWidgetItem('foo'))
        self.table.setItem(0,1,QtGui.QTableWidgetItem('bar'))
        self.table.setItem(1,0,QtGui.QTableWidgetItem('baz'))
        self.table.setItem(1,1,QtGui.QTableWidgetItem('qux'))

        layout = QtGui.QGridLayout()
        layout.addWidget(self.table, 1, 0)
        self.setLayout(layout)

        self.clip = QtGui.QApplication.clipboard()


    def keyPressEvent(self, e):
        if (e.modifiers() & QtCore.Qt.ControlModifier):
            selected = self.table.selectedRanges()

            if e.key() == QtCore.Qt.Key_C: #copy
                s = '\t'+"\t".join([str(self.table.horizontalHeaderItem(i).text()) for i in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1)])
                s = s + '\n'

                for r in xrange(selected[0].topRow(), selected[0].bottomRow()+1):
                    s += self.table.verticalHeaderItem(r).text() + '\t'
                    for c in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1):
                        try:
                            s += str(self.table.item(r,c).text()) + "\t"
                        except AttributeError:
                            s += "\t"
                    s = s[:-1] + "\n" #eliminate last '\t'
                self.clip.setText(s)