[PyQt] Paste entire column into QTableView from Excel

Mark Mordeca mark.mordeca at prosensus.ca
Mon May 13 20:12:41 BST 2013


Greetings,

I am having an issue pasting into a QTableView from Excel when the user has
copied by selecting an entire column in Excel, essentially putting every
single row in the selected column, for the entire excel sheet, on the
clipboard. The following is my paste code for QTableView:

   def paste(self):

        model=self.model()

        pasteString=QtGui.QApplication.clipboard().text()



        rows=pasteString.split('\n')

        numRows=len(rows)

        numCols=rows[0].count('\t')+1



        selectionRanges=self.selectionModel().selection()



        #make sure we only have one selection range and not non-contiguous
selections

        if len(selectionRanges)==1:

            topLeftIndex=selectionRanges[0].topLeft()

            selColumn=topLeftIndex.column()

            selRow=topLeftIndex.row()

            if selColumn+numCols>model.columnCount():

                #the number of columns we have to paste, starting at the
selected cell, go beyond how many columns exist.

                #insert the amount of columns we need to accomodate the
paste

                model.insertColumns(model.columnCount(),
numCols-(model.columnCount()-selColumn))



            if selRow+numRows>model.rowCount():

                #the number of rows we have to paste, starting at the
selected cell, go beyond how many rows exist.

                #insert the amount of rows we need to accomodate the paste

                model.insertRows(model.rowCount(),
numRows-(model.rowCount()-selRow))



            #block signals so that the "dataChanged" signal from setData
doesn't update the view for every cell we set

            model.blockSignals(True)



            for row in xrange(numRows):

                columns=rows[row].split('\t')



                [model.setData(model.createIndex(selRow+row,
selColumn+col), QVariant(columns[col])) for col in xrange(len(columns))]



            #unblock the signal and emit dataChangesd ourselves to update
all the view at once

            model.blockSignals(False)

            model.dataChanged.emit(topLeftIndex,
model.createIndex(selRow+numRows, selColumn+numCols))

This all works fine when a user has selected just a bunch of cells in Excel
and copied those. It breaks down when they select an entire column because
pasteString then becomes upwards of 1048576 characters long (this value is
found by printing pasteString.size() when highlighting a completely empty
Excel column by selecting its header and copying).  It completely kills the
program.

Is there any way to get the copied column from the clipboard more
efficiently than tab delimited text or something? Or should I just throw up
an error when the size of the string on the clipboard is some arbitrarily
large length?  Any help would be appreciated.

Thanks,

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130513/fa4de5ac/attachment.html>


More information about the PyQt mailing list