The following snippets show how one can easily interact with the clipboard from QML code with the help of a tiny C++ wrapper / adapter. This code should work for all QML versions.
The C++ code is very simple and straight forward:
#ifndef QMLCLIPBOARDADAPTER_H
#define QMLCLIPBOARDADAPTER_H
#include <QApplication>
#include <QClipboard>
#include <QObject>
class QmlClipboardAdapter : public QObject
{
Q_OBJECT
public:
explicit QmlClipboardAdapter(QObject *parent = 0) : QObject(parent) {
clipboard = QApplication::clipboard();
}
Q_INVOKABLE void setText(QString text){
clipboard->setText(text, QClipboard::Clipboard);
clipboard->setText(text, QClipboard::Selection);
}
private:
QClipboard *clipboard;
};
#endif // QMLCLIPBOARDADAPTER_H
Registration of our adapter is done as usual in the main.cpp file:
#include <QtDeclarative>
...
#include "qmlclipboardadapter.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
...
qmlRegisterType("meepasswords", 1, 0, "QClipboard");
...
}
And finally the clipboard can be used from QML code:
...
import meepasswords 1.0
...
QClipboard{
id: clipboard
}
...
onAccepted: clipboard.setText("foo")
...
Note that this implementation is a write-only clipboard. Though, modifying the above code to enable a read-write clipboard is straight forward.