0

In QTableView, copying and pasting the fields of the table is already implemented but only when the fields are in edit mode (if we double click on a field)..I would like to know how to copy and paste fields in case they are just in view mode..any hints would be appreciated.

P.S: My code is in Python..

Orcl User
  • 293
  • 1
  • 5
  • 20
  • 2
    See something like [this](http://stackoverflow.com/questions/3135737/copying-part-of-qtableview). Most likely you will have to implement it yourself. – Predelnik Feb 10 '14 at 11:07
  • This performs only the copy part..I need to copy one field and paste it to other field of the table.. – Orcl User Feb 10 '14 at 14:04
  • I can copy and paste it somewhere else like in a notepad or an excel..but I am not able to paste it to another field of the table. – Orcl User Feb 10 '14 at 14:05

2 Answers2

4

Here is a QAction I've created to copy selected cells to the clipboard. Basically, when you initialise the QAction you pass it a table widget that it binds to. When the action is triggered is gets the selected cells, sorts them into rows and columns, and copies the text to a clipboard.

class CopySelectedCellsAction(QtGui.QAction):
    def __init__(self, table_widget):
        if not isinstance(table_widget, QtGui.QTableWidget):
            raise ValueError(str('CopySelectedCellsAction must be initialised with a QTableWidget. A %s was given.' % type(table_widget)))
        super(CopySelectedCellsAction, self).__init__("Copy", table_widget)
        self.setShortcut('Ctrl+C')
        self.triggered.connect(self.copy_cells_to_clipboard)
        self.table_widget = table_widget

    def copy_cells_to_clipboard(self):
        if len(self.table_widget.selectionModel().selectedIndexes()) > 0:
            # sort select indexes into rows and columns
            previous = self.table_widget.selectionModel().selectedIndexes()[0]
            columns = []
            rows = []
            for index in self.table_widget.selectionModel().selectedIndexes():
                if previous.column() != index.column():
                    columns.append(rows)
                    rows = []
                rows.append(index.data())
                previous = index
            columns.append(rows)
            print columns       

            # add rows and columns to clipboard            
            clipboard = ""
            nrows = len(columns[0])
            ncols = len(columns)
            for r in xrange(nrows):
                for c in xrange(ncols):
                    clipboard += columns[c][r]
                    if c != (ncols-1):
                        clipboard += '\t'
                clipboard += '\n'

            # copy to the system clipboard
            sys_clip = QtGui.QApplication.clipboard()
            sys_clip.setText(clipboard)
Matt Williams
  • 1,596
  • 17
  • 24
2

You can do Excel-like copy-paste format of passing table data through clipboard: Implementation of copy() is approximately is:

    int t = model->rowCount();
    int b = -1;
    int l = model->columnCount();
    int r = -1;

    QList<QModelIndex> indexes = view->selectionModel()->selection().indexes();
    foreach(QModelIndex index, indexes) {
        t = qMin(t, index.row());
        b = qMax(b, index.row());
        l = qMin(l, index.column());
        r = qMax(r, index.column());
    }

    if ( r <0 ) return;
    if ( b <0 ) return;

    QString data = "<!--StartFragment-->\n";
    data += "<table>";

    for (int row=t;row<=b;row++) {
        data += "<tr>\n";
        for (int col=l;col<=r;col++) {
            QVariant v = model->data( model->index(row,col) );
            if ( v.canConvert(QVariant::Double) ) data += "  <td x:num>";
            else data += "  <td>";

            data += v.toString();
            data += "</td>\n";
        }
        data += "</tr>\n";
    }

    data += "</table>";
    data += "<!--EndFragment-->\n";

    QMimeData * mimeData = new QMimeData;
    mimeData->setHtml( data );
    QApplication::clipboard()->setMimeData(mimeData);

For paste() you need to implement just simple parsing of such html table. Then you can do copy-paste to/from Excel too or other table-like apps.

Another standard way (which is also supported by Excel and other apps working with table) is plain text QClipboard data tab-delimited for cell separators. Code for forming tab-delimited text is pretty similar, but parsing for paste() is even simplier.

yshurik
  • 772
  • 5
  • 12
  • Thanks..good hints in ur answer but i am writing in python and I only have problem to read the copiedItem (in the clipboard) and paste it to a filed of QTableView – Orcl User Feb 10 '14 at 16:11
  • 1
    Yes, with python the implementation can be even shorter. I just put this one as example or a starting point – yshurik Feb 10 '14 at 16:26
  • I wrote my question in a more specific way [here](http://stackoverflow.com/questions/21682261/paste-in-the-field-of-qtableview) – Orcl User Feb 10 '14 at 16:32