Issue
I have a QTextEdit widget layed out on the left and a QTextBrowser on the right, I am looking for a way to do the following:
- the user select a some text
- a function is triggered by this event
- the selected text is processed
- the processed text is displayed on the right
I have googled for it but didn't see relevant results.
Solution
You can use the selectionChanged signal to trigger the function, and then retrieve the selected text via the textCursor. The processed text can be displayed by using either setPlainText, or, if you want to use markup, setHtml. But note that QTextBrowser
only supports a limited subset of html/css.
Here's a demo script that shows how to put it all together:
from PySide import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.edit = QtGui.QTextEdit(self)
self.edit.selectionChanged.connect(self.handleSelectionChanged)
self.browser = QtGui.QTextBrowser(self)
layout = QtGui.QHBoxLayout(self)
layout.addWidget(self.edit)
layout.addWidget(self.browser)
def handleSelectionChanged(self):
text = self.edit.textCursor().selectedText()
# process text here...
self.browser.setPlainText(text)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 600, 300)
window.show()
sys.exit(app.exec_())
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.