当前位置:网站首页>QT clipboard qclipboard copy paste custom data
QT clipboard qclipboard copy paste custom data
2022-07-27 13:29:00 【mouze_】
One 、 Common data types
QClipboard Class provides an operation interface for the operating system clipboard , The most common way is to copy and paste text , As shown in the following example
QClipboard *clipboard = QGuiApplication::clipboard();
QString originalText = clipboard->text(); // Get the text in the clipboard ( Paste )
...
clipboard->setText(newText); // Write text into the clipboard ( Copy )QClipboard Several sets of functions are provided to access common data types , If the data type we want to operate on is QString、QImage or QPixmap, You can use these interfaces directly .
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)Two 、 Custom data types
If you want to write custom data types into the system clipboard , And you can read the customized data types we wrote in from the system clipboard , have access to QMimeData+QDataStream. Here is a simple example .
#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";
// Write custom data to the clipboard
setDeviceToClipboard(d1);
// Read custom data from the clipboard
struct Device *pDevice = getDeviceFromClipboard();
if (pDevice != nullptr)
{
qDebug() << pDevice->index;
qDebug() << pDevice->text;
}
return 0;
}边栏推荐
猜你喜欢

eBPF/Ftrace

Seata 在蚂蚁国际银行业务的落地实践

Preliminary discussion on NetGen and Gmsh mesh generation of any multiple sub models of CAD based on osg+occ

W3school navigation bar exercise

Background and framework introduction and basic environment preparation of hucang integrated e-commerce project

Article reproduction: srcnn

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

能说一说 Kotlin 中 lateinit 和 lazy 的区别吗?

面试考点:三种图的问题

v-show
随机推荐
Connotative quotations
力扣 1480. 一维数组的动态和 383. 赎金信412. Fizz Buzz
能说一说 Kotlin 中 lateinit 和 lazy 的区别吗?
51: Chapter 5: develop admin management services: 4: develop [add admin account, interface]; (only [user name + password, method]; [@t...] annotation controls transactions; when setting cookies, do yo
Dichotomy queries values in an array
Antd's tool function getprefixcls gets the public prefix
粘制定位
Reptile
7-16 daily sword finger offer II 041. Average value of sliding window
Redis summary: cache avalanche, cache breakdown, cache penetration and cache preheating, cache degradation
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
v-on基础指令
初学者入门:使用WordPress搭建一个专属自己的博客
利用eBPF探测Rootkit漏洞
计算字符串最后一个单词的长度,单词以空格隔开。
马斯克被曝绿了谷歌创始人:导致挚友二婚破裂,曾下跪求原谅
Write a program, accept a string consisting of letters, numbers and spaces, and a character, and then output the number of characters in the input string. Case insensitive.
电滑环的常用类型
v-show
Tools and methods - online flow chart drawing