当前位置:网站首页>Qt剪切板QClipboard 复制粘贴自定义数据
Qt剪切板QClipboard 复制粘贴自定义数据
2022-07-27 12:51:00 【mouze_】
一、常用数据类型
QClipboard类提供了对操作系统剪切板的操作接口,最常用的做法是复制粘贴文本,如下面示例
QClipboard *clipboard = QGuiApplication::clipboard();
QString originalText = clipboard->text(); //获得剪切板中的文本(粘贴)
...
clipboard->setText(newText); //往剪切板里写文本(复制)QClipboard 提供了几组函数用来访问常见的数据类型,如果我们想操作的数据类型是QString、QImage或QPixmap,可以直接使用这些接口。
QImage image(QClipboard::Mode mode = Clipboard) const
void setImage(const QImage &image, QClipboard::Mode mode = Clipboard)
QPixmap pixmap(QClipboard::Mode mode = Clipboard) const
void setPixmap(const QPixmap &pixmap, QClipboard::Mode mode = Clipboard)
QString text(QClipboard::Mode mode = Clipboard) const
QString text(QString &subtype, QClipboard::Mode mode = Clipboard) const
void setText(const QString &text, QClipboard::Mode mode = Clipboard)二、自定义数据类型
如果想将自定义的数据类型写进系统剪切板,并可以从系统剪切板读到我们写进去的自定义的数据类型,可以使用QMimeData+QDataStream。下面举一个简单示例。
#include <QApplication>
#include <QPushButton>
#include <QClipboard>
#include <QMimeData>
#include <QDebug>
struct Device
{
int index;
QString text;
};
void setDeviceToClipboard(struct Device &d)
{
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << d.index;
dataStream << d.text;
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/itemdata", itemData);
QClipboard *clipboard = QApplication::clipboard();
clipboard->setMimeData(mimeData);
}
struct Device *getDeviceFromClipboard()
{
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
if (mimeData->hasFormat("application/itemdata"))
{
QByteArray itemData = mimeData->data("application/itemdata");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
struct Device *pDevice = new struct Device;
dataStream >> pDevice->index;
dataStream >> pDevice->text;
return pDevice;
}
return nullptr;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
struct Device d1;
d1.index = 1;
d1.text = "device1";
//往剪切板写自定义数据
setDeviceToClipboard(d1);
//从剪切板读自定义数据
struct Device *pDevice = getDeviceFromClipboard();
if (pDevice != nullptr)
{
qDebug() << pDevice->index;
qDebug() << pDevice->text;
}
return 0;
}边栏推荐
- 电滑环的常用类型
- 592. Fraction addition and subtraction: introduction to expression calculation
- Tools and methods - online flow chart drawing
- SQL GROUP BY语句
- 附加:【URLEncoder.encode(待编码字符串, “编码方式“);】(是什么?;我们向cookie中设置值的时候,为什么要使用这个去编码?)(待完善……)
- AMD Adrenalin 22.7.1 驱动更新:OpenGL 性能翻倍,支持微软 Win11 22H2 系统
- Feign client automatic assembly of three clients
- 程序员培训学习后好找工作吗
- From the perspective of it, the CIO of B2B industry talks about how to change from "cost center" to "growth center"?
- PAT乙级 1109 擅长C(详解)
猜你喜欢

Sff1004-mhchxm diode sff1004

v-text

Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions

leetcode——83,24;机器学习——神经网络

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能

How can the top 500 enterprises improve their R & D efficiency? Let's see what industry experts say!

52:第五章:开发admin管理服务:5:开发【分页查询admin账号列表,接口】;(Swagger的@ApiParam(),对方法参数进行注释;PageHelper分页插件;拦截器拦截检查登录状态)

Eccv2022 | Ru & Google proposed to use clip for zero shot target detection!

Will saffron become a safe and effective natural therapy for patients with arthritis?

Article reproduction: srcnn
随机推荐
POJ2446 Chessboard【二分图最大匹配】
@Simple use of conditional
Antd's tool function getprefixcls gets the public prefix
Height collapse and BFC
Redis summary: cache avalanche, cache breakdown, cache penetration and cache preheating, cache degradation
Connotative quotations
Preliminary discussion on NetGen and Gmsh mesh generation of any multiple sub models of CAD based on osg+occ
Text style
滑环的分类以及用途
【2023复旦微电子提前批笔试题】~ 题目及参考答案
A survey of video game addictive behavior research
SQL GROUP BY语句
v-on基础指令
Forward pre check and reverse pre check
Icon Font
纵横靶场-图片的奥秘
相对定位
Feign's dynamic proxy
Musk was exposed to be the founder of Google: he broke up his best friend's second marriage and knelt down to beg for forgiveness
Can you tell me the difference between lateinit and lazy in kotlin?