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. linux 软链接 硬链接

    查看文件sun.txt   加上参数i 是显示节点 inode [root@bogon test]# ls -li sun.txt 10006225 -rw-r--r--. 1 root root 0 ...

  2. Spring框架找不到 applicationContext.xml文件,可能是由于applicationContext.xml文件的路径没有放在根目录下造成的

    Spring框架找不到 applicationContext.xml文件,可能是由于applicationContext.xml文件的路径没有放在根目录下造成的

  3. win32多线程 (二)线程同步之临界区 (critical sections)

    所谓critical sections 意指一小块“用来处理一份被共享之资源”的程序代码.你可能必须在程序的许多地方处理这一块可共享的资源.所有这些程序代码可以被同一个critical  sectio ...

  4. hdu 4269 Defend Jian Ge

    #include <cctype> #include <algorithm> #include <vector> #include <string> # ...

  5. SVN常见问题及解决方式(二)

    1.分支不同 ==> update merge(svn自动合并)2.分支冲突 ==> 协商解决冲突,选择一个正确的版本覆盖(最新的正确直接Revert最新):出现四个文件.黄色感叹号代表S ...

  6. js实现在表格中删除和添加一行

    <!DOCTYPE html><html> <head> <title> new document </title> <meta ht ...

  7. Python字符编码详解,str,bytes

    什么是明文 “明文”是可以是文本,音乐,可以编码成mp3文件.明文可以是图像的,可以编码为gif.png或jpg文件.明文是电影的,可以编码成wmv文件.不一而足. 什么是编码?把明文变成计算机语言 ...

  8. oracle数据库创建表

    实际工作中,在数据库中创建表是经常会用到的.我们今天呢?主要给大家来分享一下在数据库如何通过sql语句去创建表.其实,创建表很简单,只需要把数据库的数据类型和约束搞清楚就可以了,其他的就好说了.接下来 ...

  9. 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作

    建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...

  10. Android 基于google Zxing实现对手机中的二维码进行扫描

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/14450809 有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的 ...