Add C++ Clipboard class accessible from QML

This commit is contained in:
miruka 2019-10-25 08:36:24 -04:00
parent 7090ff601f
commit 30823a9bf5
4 changed files with 79 additions and 2 deletions

View File

@ -12,8 +12,8 @@ RCC_DIR = $$BUILD_DIR/rcc
QRC_FILE = $$BUILD_DIR/resources.qrc
RESOURCES += $$QRC_FILE
HEADERS += src/utils.h
SOURCES += src/main.cpp src/utils.cpp
HEADERS += src/utils.h src/clipboard.h
SOURCES += src/main.cpp src/utils.cpp src/clipboard.cpp
TARGET = harmonyqml

38
src/clipboard.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <QClipboard>
#include "clipboard.h"
Clipboard::Clipboard(QObject *parent)
: QObject(parent) {
connect(m_clipboard, &QClipboard::dataChanged,
this, &Clipboard::textChanged);
connect(m_clipboard, &QClipboard::selectionChanged,
this, &Clipboard::selectionChanged);
}
QString Clipboard::text() const {
return m_clipboard->text(QClipboard::Clipboard);
}
void Clipboard::setText(const QString &text) {
m_clipboard->setText(text, QClipboard::Clipboard);
}
QString Clipboard::selection() const {
return m_clipboard->text(QClipboard::Selection);
}
void Clipboard::setSelection(const QString &text) {
if (m_clipboard->supportsSelection()) {
m_clipboard->setText(text, QClipboard::Selection);
}
}
bool Clipboard::supportsSelection() const {
return m_clipboard->supportsSelection();
}

37
src/clipboard.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef CLIPBOARD_H
#define CLIPBOARD_H
#include <QGuiApplication>
#include <QObject>
class Clipboard : public QObject
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QString selection READ selection WRITE setSelection
NOTIFY selectionChanged)
Q_PROPERTY(bool supportsSelection READ supportsSelection CONSTANT)
public:
explicit Clipboard(QObject *parent = 0);
QString text() const;
void setText(const QString &text);
QString selection() const;
void setSelection(const QString &text);
bool supportsSelection() const;
signals:
void textChanged();
void selectionChanged();
private:
QClipboard *m_clipboard = QGuiApplication::clipboard();
};
#endif

View File

@ -5,6 +5,7 @@
#include <QFileInfo>
#include "utils.h"
#include "clipboard.h"
int main(int argc, char *argv[]) {
@ -25,6 +26,7 @@ int main(int argc, char *argv[]) {
#endif
objectContext->setContextProperty("CppUtils", new Utils());
objectContext->setContextProperty("Clipboard", new Clipboard());
QFileInfo qrcPath(":src/qml/Window.qml");