0

ユーザーが Excel で列全体を選択してコピーしたときに、Excel から QTableView に貼り付ける際に問題が発生しました。基本的に、選択した列のすべての行を、Excel シート全体でクリップボードに配置します。以下は、QTableView の貼り付けコードです (注意してください、これは PyQt を使用した Python ですが、原則は C++ と同じです)。

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

ユーザーがExcelで一連のセルを選択してコピーした場合、これはすべて正常に機能します。列全体を選択すると、1048576 文字を超える長さになるため、故障しますpasteString(これは、ヘッダーを選択してコピーすることで完全に空の Excel 列を強調表示するときに、pasteString.size() を出力することで見つかります)。

タブ区切りのテキストなどよりも効率的にクリップボードからコピーされた列を取得する方法はありますか? または、クリップボード上の文字列のサイズが任意に長い場合、エラーをスローする必要がありますか?

4

1 に答える 1