Issue
I want to copy & paste by QtGui.QClipboard
from QApplication()
QtGui.QClipboard
has mimedata()
method.
I can copy & paste from Web Site.
For example, I copied from here Matahari Wikipedia
I pasted it on my sample text editer window.
At first, the pasted text was as follows:
and pressed Key_1 for saving its contents.
and reexcuted my code and pressed Key_2 for loading the contents.
so...,
The saved contents are always changed to black.
Why?
I checked the html by printing the mimedata.html().
As the result of it,
<!--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-->
You can see the color is (0,0,0). So this html is black.
So, I tried to do as follows:
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)
as the result of this execution:
<!--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-->
I could make it. I could change the color (0,0.0) to (255,255,255)
And I resaved it and reloaded it, but the result was unchanged.
I want to change the contents from black to white.
What should I do?
Here is the sample code for saving and reloading.
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()
Solution
You should check toHtml() in save() method.
<!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>
You can see the background-color:#000000
So,you must change it to background-color:#FFFFFF
toHtml = self.toHtml()
toHtml = toHtml.replace("background-color:#000000;","background-color:#FFFFFF;")
out.writeQString(toHtml)
Answered By - Haru
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.