qt剪贴板

全局剪贴板

剪贴板数据更新时发送信号

1
[signal] void QClipboard::dataChanged();

获取剪贴板数据

1
2
3
const QMimeData *mimeData(QClipboard::Mode mode = Clipboard) const;
//
QApplication::clipboard()->mimeData();

设置剪贴板数据

1
2
3
void setMimeData(QMimeData *src, QClipboard::Mode mode = Clipboard);
//
QApplication::clipboard()->setMimeData(mimedata);

QMimeData

QMimeData 存储剪贴板数据

通常由对象 QDragQClipboard 使用

Tester Getter Setter MIME Types
hasText() text() setText() text/plain
hasHtml() html() setHtml() text/html
hasUrls() urls() setUrls() text/uri-list
hasImage() imageData() setImageData() image/ *
hasColor() colorData() setColorData() application/x-color
example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void MyWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
event->acceptProposedAction();
}

void MyWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasUrls()) {
foreach (QUrl url, event->mimeData()->urls()) {
...
}
}
}
存储自定义数据
  1. Custom data can be stored directly in a QMimeData object as a QByteArray using setData(). For example:

    1
    2
    3
    QByteArray csvData = ...;
    QMimeData *mimeData = new QMimeData;
    mimeData->setData("text/csv", csvData);
  2. We can subclass QMimeData and reimplement hasFormat(), formats(), and retrieveData().

  3. If the drag and drop operation occurs within a single application, we can subclass QMimeData and add extra data in it, and use a qobject_cast() in the receiver’s drop event handler.

    For example:

    1
    2
    3
    4
    5
    6
    7
    8
    void MyWidget::dropEvent(QDropEvent *event)
    {
    const MyMimeData *myData =
    qobject_cast<const MyMimeData *>(event->mimeData());
    if (myData) {
    // access myData's data directly (not through QMimeData's API)
    }
    }

QTextEdit

文本输入框的粘贴
  1. 重写该函数,返回值决定是否允许粘贴

    1
    [virtual protected] bool QTextEdit::canInsertFromMimeData(const QMimeData *source) const
  2. 重写该函数,自定义粘贴

    1
    [virtual protected] void QTextEdit::insertFromMimeData(const QMimeData *source)
作者

tty

发布于

2023-04-13

更新于

2023-05-30

许可协议