开发者

Copy selected items from QListWidget filled with filenames to the clipboard, but as files (not text)

开发者 https://www.devze.com 2023-02-10 16:55 出处:网络
I have a QListWidget which I fill with filenames, when user hits Ctrl+C I want to place the filenames to th开发者_开发技巧e clipboard, so if the user hits Ctrl+V in a file manager the files will be co

I have a QListWidget which I fill with filenames, when user hits Ctrl+C I want to place the filenames to th开发者_开发技巧e clipboard, so if the user hits Ctrl+V in a file manager the files will be copied.


You'll have to subclass the QListWidget and write in the keyPressEvent() something like that:

virtual void keyPressEvent(QKeyEvent *event) {
if (event->matches(QKeySequence::Copy)) {
  int itemsCount = count();
  QStringList strings;
  for (int i = 0; i < itemsCount; ++i)
    strings << item(i)->text();

  QApplication::clipboard()->setText(strings.join("\n"));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消