Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To actually capture the selection you use the item view's <a href="https://doc.qt.io/qt-5/qabstractitemview.html#selectionModel" rel="noreferrer">selection model</a> to get a <a href="https://doc.qt.io/qt-5/qmodelindex.html#QModelIndexList-typedef" rel="noreferrer">list of indices</a>. Given that you have a <code>QTableView *</code> called <code>view</code> you get the selection this way:</p> <pre><code>QAbstractItemModel * model = view-&gt;model(); QItemSelectionModel * selection = view-&gt;selectionModel() QModelIndexList indexes = selection-&gt;selectedIndexes(); </code></pre> <p>Then loop through the index list calling <code>model-&gt;data(index)</code> on each index. Convert the data to a string if it isn't already and concatenate each string together. Then you can use <code>QClipboard.setText</code> to paste the result to the clipboard. Note that, for Excel and Calc, each column is separated from the next by a newline ("\n") and each row is separated by a tab ("\t"). You have to check the indices to determine when you move to the next row.</p> <pre><code>QString selected_text; // You need a pair of indexes to find the row changes QModelIndex previous = indexes.first(); indexes.removeFirst(); foreach(current, indexes) { QVariant data = model-&gt;data(current); QString text = data.toString(); // At this point `text` contains the text in one cell selected_text.append(text); // If you are at the start of the row the row number of the previous index // isn't the same. Text is followed by a row separator, which is a newline. if (current.row() != previous.row()) { selected_text.append('\n'); } // Otherwise it's the same row, so append a column separator, which is a tab. else { selected_text.append('\t'); } previous = current; } QApplication.clipboard().setText(selected_text); </code></pre> <p><strong>Warning</strong>: I have not had a chance to try this code, but a PyQt equivalent works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload