Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QClipboard's setMimeData() does not work
Forum Updated to NodeBB v4.3 + New Features

QClipboard's setMimeData() does not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tubbadu
    wrote on last edited by
    #1

    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 use setText(), but I need to do this with also other mime types, so I have to use this function)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which version of Qt are you using ?
      On which platform ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Which version of Qt are you using ?
        On which platform ?

        T Offline
        T Offline
        tubbadu
        wrote on last edited by
        #3

        @SGaist hi, thanks for the answer! Sorry I forgot to mention it:
        Qt version 5.15.11 on KDE Plasma 5.27.9 (wayland)

        JonBJ 1 Reply Last reply
        0
        • T tubbadu

          @SGaist hi, thanks for the answer! Sorry I forgot to mention it:
          Qt version 5.15.11 on KDE Plasma 5.27.9 (wayland)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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. pastes something here into other applications.

          Are you able to test not using Wayland, that has various restrictions on behaviour?

          T 1 Reply Last reply
          0
          • JonBJ JonB

            @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. pastes something here into other applications.

            Are you able to test not using Wayland, that has various restrictions on behaviour?

            T Offline
            T Offline
            tubbadu
            wrote on last edited by tubbadu
            #5

            @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 that QGuiApplication::clipboard()->setText("something else"); does not work too...
            I'm building with CMake, I don't know if that makes any difference

            JonBJ 1 Reply Last reply
            0
            • T tubbadu

              @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 that QGuiApplication::clipboard()->setText("something else"); does not work too...
              I'm building with CMake, I don't know if that makes any difference

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @tubbadu If it's a bug how come it works OK for me?

              T 2 Replies Last reply
              0
              • JonBJ JonB

                @tubbadu If it's a bug how come it works OK for me?

                T Offline
                T Offline
                tubbadu
                wrote on last edited by
                #7

                @JonB this is a good question 😅

                1 Reply Last reply
                0
                • JonBJ JonB

                  @tubbadu If it's a bug how come it works OK for me?

                  T Offline
                  T Offline
                  tubbadu
                  wrote on last edited by
                  #8

                  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 "" (so QGuiApplication::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

                  SGaistS 1 Reply Last reply
                  0
                  • T tubbadu

                    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 "" (so QGuiApplication::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

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    T 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      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.

                      T Offline
                      T Offline
                      tubbadu
                      wrote on last edited by
                      #10

                      I couldn't find a solution, but as a workaround I used QProcess running wl-copy

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved