QClipboard 和 selectedText() 在 QTextEdit 中不相等

QClipboard and selectedText() are not equal in QTextEdit

我正在 QTextEdit 中做一些事情。我为 QClipboard::changed() 信号写了一个插槽。在插槽中,如何判断剪贴板中的文本是否来自我的应用程序(而不是来自外部其他应用程序)?

我的解决办法是比较剪贴板中的文本和选中的文本:

mimeData->text() == textCursor()->selectedText()

但是,我发现,当我选择多行并复制它时。 selectedText()\n 处理为 0,而 mimeData 将其处理为 \n(即 10)。所以 mimeData->text() != textCursor()->selectedText().

顺便问一下,QClipboard::ownsClipboard()是什么意思?是我要找的吗?

有什么帮助吗?谢谢!

根据文档:

bool QClipboard::ownsClipboard() const

Returns true if this clipboard object owns the clipboard data; otherwise returns false.

这就是您要找的。

clipboard = QApplication::clipboard();
    connect(clipboard, SIGNAL(changed(QClipboard::Mode)), this, SLOT(your_slot()));

广告位:

void {your class}::your_slot()
{
    if(clipboard->ownsClipboard())
        qDebug()<< "own";
    else
        qDebug()<< "not his own";
}

正在从 selectedText()

documentation 转录

Returns the current selection's text (which may be empty). This only returns the text, with no rich text formatting information. If you want a document fragment (i.e. formatted rich text) use selection() instead.

Note: If the selection obtained from an editor spans a line break, the text will contain a Unicode U+2029 paragraph separator character instead of a newline \n character. Use QString::replace() to replace these characters with newlines.