【发布时间】:2018-07-12 15:09:44
【问题描述】:
我想从QApplication()复制和粘贴QtGui.QClipboard
QtGui.QClipboard 有 mimedata() 方法。
我可以从网站复制和粘贴。
比如我从这里复制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: "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()
【问题讨论】:
-
我得到以下信息:imgur.com/a/26zAePl
-
@eyllanesc 哦...这取决于环境吗?在我的场合,我的文字总是黑色的。
-
我在 Linux 中使用 PySide 1.2.4
-
@eyllanesc 我在 Windows10 中使用 PySide 1.2.4
-
@eyllanesc 哦,我复制了维基百科的标题。你复制链接文本吗?看来你这样做了...它是蓝色文本...
标签: python pyside qclipboard