QClipboard's setMimeData() does not work
-
Hello! I'm trying to copy some data to the system clipboard using setMimeData, but I can't make it work:
QMimeData *mime = new QMimeData; mime->setText("something here"); QGuiApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard); qWarning() << QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->text();it correctly prints
"something there", but if then I hit Ctrl+V, nothing is printed, and my clipboard manager shows a blank entry. What am I doing wrong?
(I know that if I only have text I can usesetText(), but I need to do this with also other mime types, so I have to use this function) -
Hi,
Which version of Qt are you using ?
On which platform ? -
@SGaist hi, thanks for the answer! Sorry I forgot to mention it:
Qt version 5.15.11 on KDE Plasma 5.27.9 (wayland)@tubbadu
I have tested your code under Ubuntu 22.04, GNOME desktop, Xorg window manager, Qt 5.15. After executing Ctrl+V works correctly, e.g. pastessomething hereinto other applications.Are you able to test not using Wayland, that has various restrictions on behaviour?
-
@tubbadu
I have tested your code under Ubuntu 22.04, GNOME desktop, Xorg window manager, Qt 5.15. After executing Ctrl+V works correctly, e.g. pastessomething hereinto other applications.Are you able to test not using Wayland, that has various restrictions on behaviour?
@JonB just tried on the same system I mentioned on the other reply (EndeavourOS) but with X11 session, but it had the same exact behavior as on Wayland. Is it possible that it's a bug?
I also noticed thatQGuiApplication::clipboard()->setText("something else");does not work too...
I'm building with CMake, I don't know if that makes any difference -
@JonB just tried on the same system I mentioned on the other reply (EndeavourOS) but with X11 session, but it had the same exact behavior as on Wayland. Is it possible that it's a bug?
I also noticed thatQGuiApplication::clipboard()->setText("something else");does not work too...
I'm building with CMake, I don't know if that makes any difference -
Okay, I partially got it working but I cannot understand its behavior:
Instead of QClipboard, I used KSystemClipboard. If I put this code at the beginning of the main, it works, it prints""(soQGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->text()returned an empty string), but ctrl+v actually pastes "something here 1":KSystemClipboard *kkk = KSystemClipboard::instance(); QMimeData *mime = new QMimeData; mime->setText("something here 1"); kkk->setMimeData(mime, QClipboard::Clipboard); qWarning() << QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->text();problem solved, I thought. well, no, because if I copy the exact same code (with "something here 2") inside my class
Klipboard's Q_INVOKABLE method, it will behave exactly as QClipboard: it prints "something here 2", but ctrl+v pastes the last thing I copied. Is this behavior caused by the fact that I'm calling the method from QML? I have really no idea on how to solve this -
Okay, I partially got it working but I cannot understand its behavior:
Instead of QClipboard, I used KSystemClipboard. If I put this code at the beginning of the main, it works, it prints""(soQGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->text()returned an empty string), but ctrl+v actually pastes "something here 1":KSystemClipboard *kkk = KSystemClipboard::instance(); QMimeData *mime = new QMimeData; mime->setText("something here 1"); kkk->setMimeData(mime, QClipboard::Clipboard); qWarning() << QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->text();problem solved, I thought. well, no, because if I copy the exact same code (with "something here 2") inside my class
Klipboard's Q_INVOKABLE method, it will behave exactly as QClipboard: it prints "something here 2", but ctrl+v pastes the last thing I copied. Is this behavior caused by the fact that I'm calling the method from QML? I have really no idea on how to solve thisAFAIK, KDE has deeper Wayland integration than Qt so they might be doing something different with regard to the protocol required to handle the clipboard.
-
AFAIK, KDE has deeper Wayland integration than Qt so they might be doing something different with regard to the protocol required to handle the clipboard.