QClipboard.mimedata() 总是黑色背景
QClipboard.mimedata() always has black background
我想通过 QtGui.QClipboard
从 QApplication()
复制粘贴
QtGui.QClipboard
有 mimedata()
方法。
我可以从网站复制和粘贴。
例如,我从这里复制Matahari Wikipedia
我将其粘贴到示例文本编辑器中 window。
最初粘贴的文字如下:
并按下 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: "Linux Libertine","Georgia","Times",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: "Linux Libertine","Georgia","Times",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()
你应该在 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)
我想通过 QtGui.QClipboard
从 QApplication()
QtGui.QClipboard
有 mimedata()
方法。
我可以从网站复制和粘贴。
例如,我从这里复制Matahari Wikipedia
我将其粘贴到示例文本编辑器中 window。
最初粘贴的文字如下:
并按下 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: "Linux Libertine","Georgia","Times",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: "Linux Libertine","Georgia","Times",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()
你应该在 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)