<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
p.Code, li.Code, div.Code
        {mso-style-name:Code;
        mso-style-priority:99;
        mso-style-link:"Code Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:9.0pt;
        font-family:"Courier New";
        mso-fareast-language:EN-US;}
span.CodeChar
        {mso-style-name:"Code Char";
        mso-style-priority:99;
        mso-style-link:Code;
        font-family:"Courier New";}
p.Reply, li.Reply, div.Reply
        {mso-style-name:Reply;
        mso-style-link:"Reply Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.ReplyChar
        {mso-style-name:"Reply Char";
        mso-style-link:Reply;
        color:#1F497D;
        mso-fareast-language:EN-CA;}
span.EmailStyle21
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";
        mso-fareast-language:EN-CA;}
span.kwd
        {mso-style-name:kwd;}
span.pln
        {mso-style-name:pln;}
span.pun
        {mso-style-name:pun;}
span.typ
        {mso-style-name:typ;}
span.str
        {mso-style-name:str;}
span.lit
        {mso-style-name:lit;}
span.com
        {mso-style-name:com;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body lang="EN-CA" link="blue" vlink="purple"><div class="WordSection1"><p class="MsoNormal" style><span style="font-size:12.0pt;font-family:"Times New Roman","serif"">Greetings,</span></p>
<p class="MsoNormal" style><span style="font-size:12.0pt;font-family:"Times New Roman","serif"">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:</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">   def paste(self):</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        model=self.model()</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        pasteString=QtGui.QApplication.clipboard().text()</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        rows=pasteString.split('\n')</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        numRows=len(rows)</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        numCols=rows[0].count('\t')+1</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        selectionRanges=self.selectionModel().selection()</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        #make sure we only have one selection range and not non-contiguous selections</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        if len(selectionRanges)==1:</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            topLeftIndex=selectionRanges[0].topLeft()</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            selColumn=topLeftIndex.column()</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            selRow=topLeftIndex.row()</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            if selColumn+numCols>model.columnCount():</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">                #the number of columns we have to paste, starting at the selected cell, go beyond how many columns exist.</span></p><p class="MsoNormal">
<span style="font-size:10.0pt;font-family:"Courier New"">                #insert the amount of columns we need to accomodate the paste</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">                model.insertColumns(model.columnCount(), numCols-(model.columnCount()-selColumn))</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            if selRow+numRows>model.rowCount():</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">                #the number of rows we have to paste, starting at the selected cell, go beyond how many rows exist.</span></p><p class="MsoNormal">
<span style="font-size:10.0pt;font-family:"Courier New"">                #insert the amount of rows we need to accomodate the paste</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">                model.insertRows(model.rowCount(), numRows-(model.rowCount()-selRow))</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            #block signals so that the "dataChanged" signal from setData doesn't update the view for every cell we set</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            model.blockSignals(True)  </span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            for row in xrange(numRows):</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">                columns=rows[row].split('\t')</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">                [model.setData(model.createIndex(selRow+row, selColumn+col), QVariant(columns[col])) for col in xrange(len(columns))]</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""> </span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            #unblock the signal and emit dataChangesd ourselves to update all the view at once</span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            model.blockSignals(False)</span></p><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">            model.dataChanged.emit(topLeftIndex, model.createIndex(selRow+numRows, selColumn+numCols))</span></p>
<p class="MsoNormal" style><span style="font-size:12.0pt;font-family:"Times New Roman","serif"">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 </span><span style="font-size:10.0pt;font-family:"Courier New"">pasteString</span><span style="font-size:12.0pt;font-family:"Times New Roman","serif""> 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.</span></p>
<p class="MsoNormal" style><span style="font-size:12.0pt;font-family:"Times New Roman","serif"">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.</span></p>
<p class="MsoNormal" style><span style="font-size:12.0pt;font-family:"Times New Roman","serif"">Thanks,</span></p><p class="MsoNormal" style><span style="font-size:12.0pt;font-family:"Times New Roman","serif"">Mark</span></p>
<p class="MsoNormal"> </p></div></body></html>