Qt 学习之路 2(54):剪贴板

剪贴板的操作经常和前面所说的拖放技术在一起使用。大家对剪贴板都很熟悉。我们可以简单地把它理解成一个数据存储池,外面的 数据可以存进去,里面数据也可以取出来。剪贴板是由操作系统维护的,所以这提供了跨应用程序的数据交互的一种方式。Qt 已经为我们封装好很多关于剪贴板的操作,我们可以在自己的应用中很容易实现对剪贴板的支持,代码实现起来也是很简单的:

 
class ClipboardDemo : public QWidget
{
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类。这个类只有两个槽函数,一个是从剪贴板获取内容,一个是给剪贴板设置内容。

 
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()));
}

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()));
}

主界面也很简单:程序分为上下两行,上一行显示一个文本框,下一行是两个按钮,分别为设置剪贴板和读取剪贴板。最主要的代码还是在两个槽函数中:

 
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);
}

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参数。例如,我们在鼠标按键释放的事件中进行如下处理:

 
void MyTextEditor::mouseReleaseEvent(QMouseEvent *event)
{
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):剪贴板的更多相关文章

  1. Qt 学习之路 2(67):访问网络(3)

    Qt 学习之路 2(67):访问网络(3) 豆子 2013年11月5日 Qt 学习之路 2 16条评论 上一章我们了解了如何使用我们设计的NetWorker类实现我们所需要的网络操作.本章我们将继续完 ...

  2. Qt 学习之路 2(66):访问网络(2)

    Home / Qt 学习之路 2 / Qt 学习之路 2(66):访问网络(2) Qt 学习之路 2(66):访问网络(2)  豆子  2013年10月31日  Qt 学习之路 2  27条评论 上一 ...

  3. Qt 学习之路 2(53):自定义拖放数据

    Qt 学习之路 2(53):自定义拖放数据 豆子  2013年5月26日  Qt 学习之路 2  13条评论上一章中,我们的例子使用系统提供的拖放对象QMimeData进行拖放数据的存储.比如使用QM ...

  4. Qt 学习之路 2(52):使用拖放

    Qt 学习之路 2(52):使用拖放 豆子 2013年5月21日 Qt 学习之路 2 17条评论 拖放(Drag and Drop),通常会简称为 DnD,是现代软件开发中必不可少的一项技术.它提供了 ...

  5. Qt 学习之路 2(51):布尔表达式树模型

    Qt 学习之路 2(51):布尔表达式树模型 豆子 2013年5月15日 Qt 学习之路 2 17条评论 本章将会是自定义模型的最后一部分.原本打算结束这部分内容,不过实在不忍心放弃这个示例.来自于 ...

  6. Qt 学习之路 2(32):贪吃蛇游戏(2)

    Qt 学习之路 2(32):贪吃蛇游戏(2) 豆子 2012年12月27日 Qt 学习之路 2 55条评论 下面我们继续上一章的内容.在上一章中,我们已经完成了地图的设计,当然是相当简单的.在我们的游 ...

  7. Qt 学习之路 2(22):事件总结

    Qt 学习之路 2(22):事件总结 豆子 2012年10月16日 Qt 学习之路 2 47条评论 Qt 的事件是整个 Qt 框架的核心机制之一,也比较复杂.说它复杂,更多是因为它涉及到的函数众多,而 ...

  8. Qt 学习之路 2(19):事件的接受与忽略

    Home / Qt 学习之路 2 / Qt 学习之路 2(19):事件的接受与忽略 Qt 学习之路 2(19):事件的接受与忽略  豆子  2012年9月29日  Qt 学习之路 2  140条评论 ...

  9. Qt 学习之路 2(16):深入 Qt5 信号槽新语法

    Qt 学习之路 2(16):深入 Qt5 信号槽新语法  豆子  2012年9月19日  Qt 学习之路 2  53条评论 在前面的章节(信号槽和自定义信号槽)中,我们详细介绍了有关 Qt 5 的信号 ...

随机推荐

  1. errant-transactions

    https://www.percona.com/blog/2015/12/02/gtid-failover-with-mysqlslavetrx-fix-errant-transactions/ 使用 ...

  2. <c:out>标签中有一个escapeXml属性 如果为escapeXml="false",则将其中的html、xml解析出来。

    <td><c:out value="${s.name}" escapeXml="false"></c:out></td ...

  3. Solidity 没名字的function(){...}作用

    官方解释: 这个叫做fallback function,当有人 1. 只发送以太币给合约而不带任何输入数据:2. 调用smart contract时调起了一个不存在的方法.会触发执行这个方法. Wha ...

  4. 调用req.getParameter方法出现中文乱码(全是问号???)

    在java开发中,如果编码配置不统一,很容易出现中文乱码的情况,这里就记录下自己遇到的调用req.getParameter方法出现中文乱码,并解决这一情况的方法 注意修改以下几个地方 1.jsp页面中 ...

  5. ubuntu下安装配置apache2(含虚拟主机配置)

    在Ubuntu14.14中安装apache 安装指令: sudo apt-get install apache2 安装结束后: 产生的启动和停止文件是: /etc/init.d/apache2 启动: ...

  6. python 正则表达式 练习题

    会用到的语法 正则字符 释义 举例 + 前面元素至少出现一次 ab+:ab.abbbb 等 * 前面元素出现0次或多次 ab*:a.ab.abb 等 ? 匹配前面的一次或0次 Ab?: A.Ab 等 ...

  7. Position Independent Code (PIC) in shared libraries

    E原文地址:http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/下一文: ...

  8. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(5):责任链模式、观察者模式

    一.责任链模式.观察者模式 1.责任链模式:当一个对象在一条链上被多个拦截器处理(烂机器也可以选择不拦截处理它)时,我们把这样的设计模式称为责任链模式,它用于一个对象在多个角色中传递的场景.   2. ...

  9. ArcGIS Desktop和Engine中对点要素图层Graduated Symbols渲染的实现 Rotation Symbol (转)

    摘要         ArcGIS中,对于要素图层的渲染,支持按照要素字段的值渲染要素的大小,其中Graduated Symbols可以对大小进行分级渲染.在个人开发系统的过程中,也可以用来美化数据显 ...

  10. DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题

    使用DPFJAlertDialog控件时发现DPFJAlertDialog1Click不能捕获到对话框到底按了那个按键,上网搜索后找到了解决方法: 打开DPF.Android.JAlertDialog ...