关于QML监控系统粘贴板的问题

因为最近要搞android的PDA的物理扫描枪的程序,PDA扫到的条码会直接填入输入框,尝试了多种方法无果后最终尝试用监控粘贴板变化的方式。 

代码如下:


#ifndef QCLIPBOARDPROXY_H
#define QCLIPBOARDPROXY_H

#include 

class QClipboard;

class QClipboardproxy : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString text READ text NOTIFY textChanged)

public:
    explicit QClipboardproxy(QClipboard *c);

    QString text();

signals:
    void textChanged();

public slots:
    void debugMyMsg();

private:
    QClipboard* clipboard;
};

#endif // QCLIPBOARDPROXY_H

QClipboardproxy.cpp

#include "QClipboardproxy.h"

#include 
#include 

QClipboardproxy::QClipboardproxy(QClipboard *c):clipboard(c)
{
    connect(c, &QClipboard::dataChanged, this, &QClipboardproxy::textChanged);
    connect(c, &QClipboard::dataChanged, this, &QClipboardproxy::debugMyMsg);
}

QString QClipboardproxy::text()
{
    return clipboard->text();
}

void QClipboardproxy::debugMyMsg()
{
    qDebug() << "get the clipboard change";
}

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    //ready engin
    QQmlApplicationEngine engine;

    //set var
    engine.rootContext()->setContextProperty("g_proxy",  new QClipboardproxy());

    //show
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    //step 1

    //step 2
    g_Dev_WebService->initDev();

    return app.exec();
}

main.qml


直接绑定就OK,例如 

Text{

        text: g_proxy.text

}


你可能感兴趣的:(Qt)