Hello. There is a QTableView. Here it is . I want to copy the entire line from it, but only the value from which I began to select is copied. If you put selectionBehavior on a string, the last value of the string is copied.

I also tried to make a single line and put it on the clipboard - it fits in the end. So how do I get the entire row from QTableView?

    1 answer 1

    I translate the answer from here :

    To actually capture a selection, use a QItemSelectionModel for a list of indexes. Suppose there is a view pointer of type QTableView* , you can get a sample like this:

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

    Then go through the list of indexes, calling model->data(index) for each index. Convert the data to a string if it is not yet a string, and add all the strings together. Then use QClipboard.setText to place the text on the clipboard. Note that for Excel and Calc, each column must be separated from the next by a tab ( "\t" ), and each line is separated by a newline ( "\n" ). You need to look at the index when going to the next line.

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

    NB: I did not check this code, but it works for PyQt.

    • Yes, I then ferried the clipboard, it earned. Thank you for finding the post. And about the copying from the table itself (in terms of even without setting up the slots) there is nothing? - psy_duck