Qt6 QClipboard doesn't put anything in the clipboard (Linux)

179 views Asked by At

I've been trying to get a simple app to work on Linux using QClipboard to copy some information to the system clipboard. The code that does the copy to clipboard is very simple:

        qDebug() << "Copying to clipboard:" << result;
        QClipboard *clipboard = QGuiApplication::clipboard();
        qDebug() << "Existing content (to be overwritten):" << clipboard->text(QClipboard::Clipboard);
        clipboard->setText(result, QClipboard::Clipboard);
        qDebug() << "Clipboard contents:" << clipboard->text(QClipboard::Clipboard);

When it runs, the printed clipboard contents before setText show the existing contents of the clipboard and the printed clipboard contents after setText show the text that should have been copied to the clipboard. All looks okay. However, nothing is in the clipboard. If I run the same thing again, the existing clipboard contents is shown as empty:

$ ./output/linux/digraph # Then type "VE" and click "Copy to Clipboard"
Copying to clipboard: "Version: v1.0.3-15-4700916fe26d\nCompiled 8th January 2023"
Existing content (to be overwritten): "Manually copied to clipboard"
Clipboard contents: "Version: v1.0.3-15-4700916fe26d\nCompiled 8th January 2023"

# But clipboard is empty!

$ ./output/linux/digraph # Then type "VE" and click "Copy to Clipboard"
Copying to clipboard: "Version: v1.0.3-15-4700916fe26d\nCompiled 8th January 2023"
Existing content (to be overwritten): ""
Clipboard contents: "Version: v1.0.3-15-4700916fe26d\nCompiled 8th January 2023"

# Clipboard still empty!

$ ./output/linux/digraph # Then type "VE" and click "Copy to Clipboard"
Copying to clipboard: "Version: v1.0.3-15-4700916fe26d\nCompiled 8th January 2023"
Existing content (to be overwritten): ""
Clipboard contents: "Version: v1.0.3-15-4700916fe26d\nCompiled 8th January 2023"

# Clipboard still empty!

Is there something else I need to do to make the written clipboard contents be visible to other applications and to persist after the application quits?

The full source code of the application is here: https://github.com/abudden/digraph ; it works on Windows (but uses the Windows API to do the copy as I found QClipboard worked but was unreliable) and is arguably a bit pointless on Linux with Compose Key type features, but someone asked me if I could make it work on Linux, so I'm trying.

0

There are 0 answers