Berschi Berschi - 1 year ago 528
C++ Question

selected Rows/Line in QTableView copy to QClipboard

First of all, sorry for bad English.

It's about C++ and Qt. I have a SQLite-Database and I did it into a QSqlTableModel.
To show the Database, I put that Model into a QTableView.

Now I want to create a Method where the selected Rows (or the whole Line) will be copied into the QClipboard. After that I want to insert it into my OpenOffice.Calc-Document.

But I have no Idea what to do with the "Selected"-SIGNAL and the QModelIndex and how to put this into the Clipboard.

So can you please help me?


Answer Source

To actually capture the selection you use the item view's selection model to get a list of indices. Given that you have a QTableView * called view you get the selection this way:

QAbstractItemModel * model = view->model();
QItemSelectionModel * selection = view->selectionModel()
QModelIndexList indexes = selection->selectedIndexes();

Then loop through the index list calling model->data(index) on each index. Convert the data to a string if it isn't already and concatenate each string together. Then you can use QClipboard.setText 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.

QString selected_text;
// You need a pair of indexes to find the row changes
QModelIndex previous = indexes.first();
foreach(current, indexes)
    QVariant data = model->data(current);
    QString text = data.toString();
    // At this point `text` contains the text in one cell
    // 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())
    // Otherwise it's the same row, so append a column separator, which is a tab.
    previous = current;

Warning: I have not had a chance to try this code, but a PyQt equivalent works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download