【问题标题】:QClipboard.mimedata() always has black backgroundQClipboard.mimedata() 总是有黑色背景
【发布时间】:2018-07-12 15:09:44
【问题描述】:

我想从QApplication()复制和粘贴QtGui.QClipboard

QtGui.QClipboardmimedata() 方法。

我可以从网站复制和粘贴。

比如我从这里复制Matahari Wikipedia

我将它粘贴到我的示例文本编辑器窗口中。

一开始粘贴的文字如下:

并按下 Key_1 以保存其内容。

并重新执行我的代码并按下 Key_2 以加载内容。

所以...,

保存的内容总是变成黑色的。

为什么?

我通过打印 mimedata.html() 检查了 html。

结果,

<!--StartFragment--><span style="display: inline !important; float: none; background-color: transparent; color: rgb(0, 0, 0); font-family: &quot;Linux Libertine&quot;,&quot;Georgia&quot;,&quot;Times&quot;,serif; font-size: 28.8px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 37.44px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;">Matahari</span><!--EndFragment-->

你可以看到颜色是(0,0,0)。所以这个html是黑色的。

所以,我尝试如下:

clipboard = QtGui.QApplication.clipboard()                         
html = clipboard.mimeData().html()
print(html)
html = html.replace("color: rgb(0, 0, 0);","color: rgb(255, 255, 255);")
clipboard.mimeData().setHtml(html)

作为这次执行的结果:

<!--StartFragment--><span style="display: inline !important; float: none; background-color: transparent; color: rgb(255, 255, 255); font-family: &quot;Linux Libertine&quot;,&quot;Georgia&quot;,&quot;Times&quot;,serif; font-size: 28.8px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 37.44px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;">Matahari</span><!--EndFragment-->

我能做到。我可以将颜色 (0,0.0) 更改为 (255,255,255)

我重新保存并重新加载它,但结果没有改变。

我想把内容从黑色改成白色。

我该怎么办?

这里是保存和重新加载的示例代码。

from PySide import QtGui

from PySide import QtCore
import sys
import os
class TextEdit(QtGui.QTextEdit):
    def __init__(self,parent=None):
        super(TextEdit,self).__init__(parent=None)
    def keyPressEvent(self,event):
        if event.key() == QtCore.Qt.Key_1:
            self.save()
            return
        elif event.key() == QtCore.Qt.Key_2:
            self.load()
            return
        elif event.key() == QtCore.Qt.Key_V:
            self.copy_paste()
            return
        return QtGui.QTextEdit.keyPressEvent(self,event)
    def save(self):
        print(os.getcwd()+"copy_paste_test.dat")
        file = QtCore.QFile(os.getcwd()+"copy_paste_test.dat")
        file.open(QtCore.QFile.ReadWrite)
        out = QtCore.QDataStream(file)
        out.writeQString(self.toHtml())
        file.close()
    def load(self):
        file = QtCore.QFile(os.getcwd()+"copy_paste_test.dat")
        file.open(QtCore.QFile.ReadOnly)
        out = QtCore.QDataStream(file)
        self.insertHtml(out.readQString())
        file.close()
    def copy_paste(self):

        clipboard = QtGui.QApplication.clipboard()
        self.insertFromMimeData(clipboard.mimeData())      

def main():
    try:
        QtGui.QApplication([])
    except Exception as e:
        print(e)
    textedit = TextEdit()
    textedit.show()
    sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
    main()

【问题讨论】:

  • 我得到以下信息:imgur.com/a/26zAePl
  • @eyllanesc 哦...这取决于环境吗?在我的场合,我的文字总是黑色的。
  • 我在 Linux 中使用 PySide 1.2.4
  • @eyllanesc 我在 Windows10 中使用 PySide 1.2.4
  • @eyllanesc 哦,我复制了维基百科的标题。你复制链接文本吗?看来你这样做了...它是蓝色文本...

标签: python pyside qclipboard


【解决方案1】:

你应该在 save() 方法中检查 toHtml()。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS UI Gothic'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Linux Libertine,Georgia,Times,serif'; color:#000000; background-color:#000000;">Matahari</span></p></body></html>

你可以看到背景颜色:#000000

因此,您必须将其更改为背景颜色:#FFFFFF

toHtml = self.toHtml()
toHtml = toHtml.replace("background-color:#000000;","background-color:#FFFFFF;")
out.writeQString(toHtml)

【讨论】:

  • 听说有 png ,透明色对背景有影响...我指定哪种颜色为透明色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
相关资源
最近更新 更多