PDA

View Full Version : QListView/QListWidget: copy multiple items and paste elsewhere



dhalbert
19th November 2010, 21:21
I have a QListWidget with its selectionMode set to QAbstractView.ExtendedSelection. So I can select multiple items (rows) in the QListWidget.

If I select multiple items, press Copy (Ctrl+C), and then paste the result elsewhere, either in a Qt text widget or in some non-Qt place, I get the text of only the last item. How can I get the selection to include the text of all the selected items, separated by newlines or something like that?

franz
22nd November 2010, 11:34
Based on gut feeling I'd say that the QListWidget only produces the mime-data for the currently selected item. I could be wrong there. However, you can overload the mimeData() function and adapt it to your needs.

dhalbert
22nd November 2010, 21:49
It wasn't mimeData(), but franz set me a track of where to look. QAbstractItemView::keyPressEvent() handles Ctrl+C:

if (event == QKeySequence::Copy) {
QVariant variant;
if (d->model)
variant = d->model->data(currentIndex(), Qt::DisplayRole);
if (variant.type() == QVariant::String)
QApplication::clipboard()->setText(variant.toString());
event->accept();
}

You can see that it gets some string data from currentIndex(), which is a single item: it's not going to handle multiple selected items. So I have to subclass QListWidget and reimplement keyPressEvent() to make this work the way I want.