Qt 学习之路 2(54):剪贴板
Qt 学习之路 2(54):剪贴板
剪贴板的操作经常和前面所说的拖放技术在一起使用。大家对剪贴板都很熟悉。我们可以简单地把它理解成一个数据存储池,外面的 数据可以存进去,里面数据也可以取出来。剪贴板是由操作系统维护的,所以这提供了跨应用程序的数据交互的一种方式。Qt 已经为我们封装好很多关于剪贴板的操作,我们可以在自己的应用中很容易实现对剪贴板的支持,代码实现起来也是很简单的:
{
Q_OBJECT
public:
ClipboardDemo(QWidget *parent = 0);
private slots:
void setClipboardContent();
void getClipboardContent();
};
|
1
2
3
4
5
6
7
8
9
|
class ClipboardDemo : public QWidget
{
Q_OBJECT
public:
ClipboardDemo(QWidget *parent = 0);
private slots:
void setClipboardContent();
void getClipboardContent();
};
|
我们定义了一个ClipboardDemo类。这个类只有两个槽函数,一个是从剪贴板获取内容,一个是给剪贴板设置内容。
: QWidget(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *northLayout = new QHBoxLayout;
QHBoxLayout *southLayout = new QHBoxLayout;
QTextEdit *editor = new QTextEdit;
QLabel *label = new QLabel;
label->setText("Text Input: ");
label->setBuddy(editor);
QPushButton *copyButton = new QPushButton;
copyButton->setText("Set Clipboard");
QPushButton *pasteButton = new QPushButton;
pasteButton->setText("Get Clipboard");
northLayout->addWidget(label);
northLayout->addWidget(editor);
southLayout->addWidget(copyButton);
southLayout->addWidget(pasteButton);
mainLayout->addLayout(northLayout);
mainLayout->addLayout(southLayout);
connect(copyButton, SIGNAL(clicked()), this, SLOT(setClipboardContent()));
connect(pasteButton, SIGNAL(clicked()), this, SLOT(getClipboardContent()));
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
ClipboardDemo::ClipboardDemo(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *northLayout = new QHBoxLayout;
QHBoxLayout *southLayout = new QHBoxLayout;
QTextEdit *editor = new QTextEdit;
QLabel *label = new QLabel;
label->setText("Text Input: ");
label->setBuddy(editor);
QPushButton *copyButton = new QPushButton;
copyButton->setText("Set Clipboard");
QPushButton *pasteButton = new QPushButton;
pasteButton->setText("Get Clipboard");
northLayout->addWidget(label);
northLayout->addWidget(editor);
southLayout->addWidget(copyButton);
southLayout->addWidget(pasteButton);
mainLayout->addLayout(northLayout);
mainLayout->addLayout(southLayout);
connect(copyButton, SIGNAL(clicked()), this, SLOT(setClipboardContent()));
connect(pasteButton, SIGNAL(clicked()), this, SLOT(getClipboardContent()));
}
|
主界面也很简单:程序分为上下两行,上一行显示一个文本框,下一行是两个按钮,分别为设置剪贴板和读取剪贴板。最主要的代码还是在两个槽函数中:
{
QClipboard *board = QApplication::clipboard();
board->setText("Text from Qt Application");
}
void ClipboardDemo::getClipboardContent()
{
QClipboard *board = QApplication::clipboard();
QString str = board->text();
QMessageBox::information(NULL, "From clipboard", str);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
void ClipboardDemo::setClipboardContent()
{
QClipboard *board = QApplication::clipboard();
board->setText("Text from Qt Application");
}
void ClipboardDemo::getClipboardContent()
{
QClipboard *board = QApplication::clipboard();
QString str = board->text();
QMessageBox::information(NULL, "From clipboard", str);
}
|
槽函数也很简单。我们使用QApplication::clipboard()函数获得系统剪贴板对象。这个函数的返回值是QClipboard指针。我们可以从这个类的 API 中看到,通过setText(),setImage()或者setPixmap()函数可以将数据放置到剪贴板内,也就是通常所说的剪贴或者复制的操作;使用text(),image()或者pixmap()函数则可以从剪贴板获得数据,也就是粘贴。
另外值得说的是,通过上面的例子可以看出,QTextEdit默认就支持 Ctrl+C, Ctrl+V 等快捷键操作的。不仅如此,很多 Qt 的组件都提供了很方便的操作,因此我们需要从文档中获取具体的信息,从而避免自己重新去发明轮子。
QClipboard提供的数据类型很少,如果需要,我们可以继承QMimeData类,通过调用setMimeData()函数让剪贴板能够支持我们自己的数据类型。具体实现我们已经在前面的章节中有过介绍,这里不再赘述。
在 X11 系统中,鼠标中键(一般是滚轮)可以支持剪贴操作。为了实现这一功能,我们需要向QClipboard::text()函数传递QClipboard::Selection参数。例如,我们在鼠标按键释放的事件中进行如下处理:
{
QClipboard *clipboard = QApplication::clipboard();
if (event->button() == Qt::MidButton
&& clipboard->supportsSelection()) {
QString text = clipboard->text(QClipboard::Selection);
pasteText(text);
}
}
|
1
2
3
4
5
6
7
8
9
|
void MyTextEditor::mouseReleaseEvent(QMouseEvent *event)
{
QClipboard *clipboard = QApplication::clipboard();
if (event->button() == Qt::MidButton
&& clipboard->supportsSelection()) {
QString text = clipboard->text(QClipboard::Selection);
pasteText(text);
}
}
|
这里的supportsSelection()函数在 X11 平台返回 true,其余平台都是返回 false。这样,我们便可以为 X11 平台提供额外的操作。
另外,QClipboard提供了dataChanged()信号,以便监听剪贴板数据变化。
Qt 学习之路 2(54):剪贴板的更多相关文章
- Qt 学习之路 2(67):访问网络(3)
Qt 学习之路 2(67):访问网络(3) 豆子 2013年11月5日 Qt 学习之路 2 16条评论 上一章我们了解了如何使用我们设计的NetWorker类实现我们所需要的网络操作.本章我们将继续完 ...
- Qt 学习之路 2(66):访问网络(2)
Home / Qt 学习之路 2 / Qt 学习之路 2(66):访问网络(2) Qt 学习之路 2(66):访问网络(2) 豆子 2013年10月31日 Qt 学习之路 2 27条评论 上一 ...
- Qt 学习之路 2(53):自定义拖放数据
Qt 学习之路 2(53):自定义拖放数据 豆子 2013年5月26日 Qt 学习之路 2 13条评论上一章中,我们的例子使用系统提供的拖放对象QMimeData进行拖放数据的存储.比如使用QM ...
- Qt 学习之路 2(52):使用拖放
Qt 学习之路 2(52):使用拖放 豆子 2013年5月21日 Qt 学习之路 2 17条评论 拖放(Drag and Drop),通常会简称为 DnD,是现代软件开发中必不可少的一项技术.它提供了 ...
- Qt 学习之路 2(51):布尔表达式树模型
Qt 学习之路 2(51):布尔表达式树模型 豆子 2013年5月15日 Qt 学习之路 2 17条评论 本章将会是自定义模型的最后一部分.原本打算结束这部分内容,不过实在不忍心放弃这个示例.来自于 ...
- Qt 学习之路 2(32):贪吃蛇游戏(2)
Qt 学习之路 2(32):贪吃蛇游戏(2) 豆子 2012年12月27日 Qt 学习之路 2 55条评论 下面我们继续上一章的内容.在上一章中,我们已经完成了地图的设计,当然是相当简单的.在我们的游 ...
- Qt 学习之路 2(22):事件总结
Qt 学习之路 2(22):事件总结 豆子 2012年10月16日 Qt 学习之路 2 47条评论 Qt 的事件是整个 Qt 框架的核心机制之一,也比较复杂.说它复杂,更多是因为它涉及到的函数众多,而 ...
- Qt 学习之路 2(19):事件的接受与忽略
Home / Qt 学习之路 2 / Qt 学习之路 2(19):事件的接受与忽略 Qt 学习之路 2(19):事件的接受与忽略 豆子 2012年9月29日 Qt 学习之路 2 140条评论 ...
- Qt 学习之路 2(16):深入 Qt5 信号槽新语法
Qt 学习之路 2(16):深入 Qt5 信号槽新语法 豆子 2012年9月19日 Qt 学习之路 2 53条评论 在前面的章节(信号槽和自定义信号槽)中,我们详细介绍了有关 Qt 5 的信号 ...
随机推荐
- vbs获取html内容
Dim content,name,password,arr,pos msg1="请输入ip和端口号地址"&chr(13)&chr(10)&"如ht ...
- Redis搭建(四):Sharding集群模式
一. 方案 1. 介绍redis集群分为服务端集群(Cluster)和客户端分片(Sharding)服务端集群:redis3.0以上版本实现,使用哈希槽,计算key的CRC16结果再模16834.此处 ...
- Dubbo简单介绍及其和zookeeper的关系
何为Dubbo Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维 ...
- 用eclipse+svn插件,上传新项目到svn服务器
给定trunk路径,https://svn.ws.125089.com/public/nlp/3434index/IndexByModelSolr/trunk/. 其中自己的web项目名字是Index ...
- 使用Fuel安装openstack
一.前言 Fuel是OpenStack的开源部署和管理工具.作为OpenStack社区的开发贡献者,它为OpenStack.OpenStack相关社区项目以及OpenStack插件的部署和管理提供了直 ...
- SpringBoot26 RestTemplate、WebClient
1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...
- PCL struct“flann::SearchParams参数错误
最近在使用PCL的KdTreeFLANN的时候报错:error C2079: “pcl::KdTreeFLANN<PointT>::param_radius_”使用未定义的 struct“ ...
- Marvel
Marvel and what it is From http://www.tuicool.com/articles/qA3yau With marvel you can get an overvie ...
- 杀毒软件 avg
http://filehippo.com/download_avg_antivirus_64 R studio: https://www.rstudio.com/products/rstudio/do ...
- 手打的table
突然觉得,如果我不上传源码和写篇博客,对不起花在这个破网页2个小时的时间,完全手打,浏览器调效果. 源码如下: a.html: <!DOCTYPE html PUBLIC "-//W3 ...