I'm making a text editor using Qt and Python(PySide2). Like most other text editors, I want to make visible whitespaces(space, tab, new line...) and succeed it. QTextEdit and QTextOption show whitespace characters as visible special characters. (space to dot, tab to arrow, new line to reverse-P)
But the colors of those special characters are same with other characters. I think it will be more readable if the colors are different.
How to change the color of the special characters of whitespaces?
orgText = """
\t\tAll those moments
\t\twill be lost
in time
like tears
in rain.
It's time to die."""
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.teOrg.setPlainText(orgText)
self.teOrg.document().setDefaultTextOption(option)
If you want to give a special based on the text then you should use QSyntaxHighlighter. To do this you must get the start and end of the pattern and change the format using setFormat().
I could only modify the format of the space (" ") and the tab ("\t") but not the paragraph separators(¶):
import re
from PySide2 import QtCore, QtGui, QtWidgets
orgText = """
\t\tAll those moments
\t\twill be lost
in time
like tears
in rain.
It's time to die."""
class Highlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
space_format = QtGui.QTextCharFormat()
space_format.setBackground(QtGui.QColor("salmon"))
tab_format = QtGui.QTextCharFormat()
tab_format.setBackground(QtGui.QColor("lightgray"))
self.highlightingRules = [
(r"( )\1*", space_format),
(r"(\t)\1*", tab_format),
]
def highlightBlock(self, text):
for pattern, fmt in self.highlightingRules:
expression = re.compile(pattern)
m = expression.search(text)
while m is not None:
start, end = m.span()
self.setFormat(start, end - start, fmt)
m = expression.search(text, end + 1)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.teOrg = QtWidgets.QTextEdit()
self.setCentralWidget(self.teOrg)
self.teOrg.setPlainText(orgText)
option = QtGui.QTextOption()
option.setFlags(
QtGui.QTextOption.ShowTabsAndSpaces
| QtGui.QTextOption.ShowLineAndParagraphSeparators
)
self.teOrg.document().setDefaultTextOption(option)
self.highlighter = Highlighter(self.teOrg.document())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(320, 240)
w.show()
sys.exit(app.exec_())
Finally, I rewrote #eyllanesc's code.
import sys
import typing
import re
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class SyntaxHighlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent:typing.Union[QtCore.QObject, QtGui.QTextDocument, None]=None):
super().__init__(parent)
self.spaceFmt = QtGui.QTextCharFormat()
self.spaceFmt.setForeground(QtGui.QColor('red'))
self.expression = re.compile(r'\s+', re.U | re.S | re.M)
def highlightBlock(self, text:str):
for match in self.expression.finditer(text):
start, end = match.span()
self.setFormat(start, end - start, self.spaceFmt)
class TextEditWin(QtWidgets.QMainWindow):
def __init__(self):
# Initialize ui.
super().__init__()
self.resize(800, 600)
self.textEdit = QtWidgets.QTextEdit(self)
self.setCentralWidget(self.textEdit)
self.textEdit.setFontPointSize(15)
# Make space characters visible.
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.textEdit.document().setDefaultTextOption(option)
# Change color of space characters.
self.highlighter = SyntaxHighlighter(self.textEdit.document())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = TextEditWin()
win.show()
sys.exit(app.exec_())
My code do not change color of the paragraph separators(¶) either. I think it is because Qt delivers the text except '\n' when highlightBlock() called.
Thanks #eyllanesc.
Related
How to work with setFormats?
The program displays QTextEdit in QMainWindow.
The task is to find the word "import" and highlight it in red using block.layout.setFormats (I don't want the undo-redo history to include the appearance change, and I don't want to use QSyntaxHighlighter).
I don't understand why when finding the word "import" and then setFormats, the corresponding block becomes invisible.
from PySide6 import QtWidgets, QtCore, QtGui
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.central_widget = QtWidgets.QWidget(self)
self.central_widget.setLayout(QtWidgets.QVBoxLayout(self.central_widget))
self.text_editor = QtWidgets.QTextEdit(self.central_widget)
self.central_widget.layout().addWidget(self.text_editor)
self.setCentralWidget(self.central_widget)
self.text_editor.setFont(QtGui.QFont('Arial', 14))
self.text_editor.textChanged.connect(self.text_changed)
#QtCore.Slot()
def text_changed(self):
word = 'import'
text_cursor_before_find_op = self.text_editor.textCursor()
self.text_editor.moveCursor(QtGui.QTextCursor.MoveOperation.Start)
found = self.text_editor.find(word)
if found:
text_cursor = self.text_editor.textCursor()
text_cursor.setPosition(text_cursor.position())
block = text_cursor.block()
position_in_block = text_cursor.positionInBlock() - len(word)
format_range = QtGui.QTextLayout.FormatRange()
format_range.start = position_in_block
format_range.length = len(word)
format_range.format = self.text_editor.currentCharFormat()
format_range.format.setForeground(QtGui.QColor('#FF0000'))
formats = [format_range]
block.layout().setFormats(formats)
print(position_in_block,
repr(block.text()),
(format_range.start, format_range.length, format_range.format),
block.isValid(), block.isVisible(),
sep='\n', end='\n\n')
self.text_editor.setTextCursor(text_cursor_before_find_op)
app = QtWidgets.QApplication()
window = MainWindow()
window.show()
app.exec()
I have no idea why block.layout.setFormats() does not work.
But if your intention is to highlight the word "import", then you might want to use something like this.
from PySide6 import QtWidgets, QtCore, QtGui
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.central_widget = QtWidgets.QWidget(self)
self.central_widget.setLayout(QtWidgets.QVBoxLayout(self.central_widget))
self.text_editor = QtWidgets.QTextEdit(self.central_widget)
self.central_widget.layout().addWidget(self.text_editor)
self.setCentralWidget(self.central_widget)
self.text_editor.setFont(QtGui.QFont('Arial', 14))
self.text_editor.textChanged.connect(self.text_changed)
#QtCore.Slot()
def text_changed(self):
word = 'import'
text_cursor = self.text_editor.document().find(word)
if text_cursor:
extraSelection = QtWidgets.QTextEdit.ExtraSelection()
extraSelection.cursor = text_cursor
extraSelection.format = self.text_editor.currentCharFormat()
extraSelection.format.setForeground(QtGui.QColor('#FF0000'))
self.text_editor.setExtraSelections([extraSelection])
else:
self.text_editor.setExtraSelections([])
app = QtWidgets.QApplication()
window = MainWindow()
window.show()
app.exec()
Of course this has some limitations, e.g. finding only the first occurrence in the whole document, not checking if the word is delimited by whitespace, keeping format after changing the word to something else etc. But you will need to resolve these yourself. Your original coude would have the same limitations.
I have a QLineEdit widget to enter text (a simple search pattern) that can include spaces.
You can't see the space characters, this is especially obvious for trailing spaces. I know you can set option ShowTabsAndSpaces by calling method setDefaultTextOption on the document of a QTextEdit widget, but is there a way to set this option (or something similar) on a QLineEdit widget?
Method setDefaultTextOption is not available for QLineEdit widgets, so I tried:
item = QLineEdit(value)
option = QTextOption()
option.setFlags(QTextOption.ShowTabsAndSpaces)
item.initStyleOption(option)
But that gives me an exception:
<class 'TypeError'>:
'PySide2.QtWidgets.QLineEdit.initStyleOption' called with wrong argument types:
PySide2.QtWidgets.QLineEdit.initStyleOption(PySide2.QtGui.QTextOption)
Supported signatures:
PySide2.QtWidgets.QLineEdit.initStyleOption(PySide2.QtWidgets.QStyleOptionFrame)
How do I set option ShowTabsAndSpaces on a QLineEdit widget or is there another way to make space characters visible in a QLineEdit widget. I would be happy with only space characters that are visible.
Used the suggestion of #ekhumoro to port https://stackoverflow.com/a/56298439/4459346
This resulted in:
#!/usr/bin/env python3
import sys
import time
from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication,
QVBoxLayout, QDialog, QMessageBox, QPlainTextEdit, QGridLayout)
from PySide2.QtCore import (Qt, QMimeData)
from PySide2.QtGui import (QTextOption, QFontMetrics)
# source: https://stackoverflow.com/a/56298439/4459346
class SpacesLineEdit(QPlainTextEdit):
def __init__(self, text=None):
if text:
super().__init__(text)
else:
super().__init__()
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setLineWrapMode(QPlainTextEdit.NoWrap)
self.setTabChangesFocus(True)
option = QTextOption()
option.setFlags(QTextOption.ShowTabsAndSpaces)
self.document().setDefaultTextOption(option)
# limit height to one line
self.setHeight(1)
# Stealing the sizeHint from a plain QLineEdit will do for now :-P
self._sizeHint = QLineEdit().sizeHint()
def minimumSizeHint(self):
return self._sizeHint
def sizeHint(self):
return self._sizeHint
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
event.ignore()
return
super().keyPressEvent(event)
def insertFromMimeData(self, source):
text = source.text()
text = text.replace(str('\r\n'), str(' '))
text = text.replace(str('\n'), str(' '))
text = text.replace(str('\r'), str(' '))
processedSource = QMimeData()
processedSource.setText(text)
super().insertFromMimeData(processedSource)
def setText(self, text):
self.setPlainText(text)
def text(self):
return self.toPlainText()
def setHeight1(self):
# see
doc = self.document()
b = doc.begin()
layout = doc.documentLayout()
h = layout.blockBoundingRect(b).height()
self.setFixedHeight(h + 2 * self.frameWidth() + 1)
def setHeight(self, nRows):
# source: https://stackoverflow.com/a/46997337/4459346
pdoc = self.document()
fm = QFontMetrics(pdoc.defaultFont())
lineSpacing = fm.lineSpacing()
print(lineSpacing) # todo:test
margins = self.contentsMargins()
nHeight = lineSpacing * nRows + \
(pdoc.documentMargin() + self.frameWidth()) * 2 + \
margins.top() + margins.bottom()
self.setFixedHeight(nHeight)
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
demoText = " some text with spaces "
self.lineedit0 = QLineEdit(demoText)
self.lineedit1 = SpacesLineEdit(demoText)
self.lineedit2 = QLineEdit(demoText)
self.lineedit3 = QLineEdit(demoText)
layout = QGridLayout()
layout.addWidget(self.lineedit0, 0, 0)
layout.addWidget(self.lineedit1, 1, 0)
layout.addWidget(self.lineedit2, 0, 1)
layout.addWidget(self.lineedit3, 1, 1)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
form = Form()
print('starting app...')
form.show()
sys.exit(app.exec_())
The spaces are now shown, just as I wanted.
The only thing I had to add was a method to set the height. See method setHeight, which I copied from https://stackoverflow.com/a/46997337/4459346.
The original method tries to calculate the exact line height, but seems it doesn't get the height exactly right:
The one on the bottom-left is the QPlainTextEdit, the other three are just qlineEdit widgets.
I can fix the incorrect height by subtracting a few pixels, but I rather not do that without knowing what I'm doing.
Anyone, any suggestions to improve the code in method setHeight?
I am trying to display console output of a python script in a QplainTextEdit widget in PyQt5.
I am getting this error:
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
I have defined my objects in the pyqt GUI file and I believe that I have all the imports.
Update
I have amended the code in this question:
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtWidgets import QFileDialog, QPlainTextEdit
from PyQt5 import QtCore, QtGui, QtWidgets
from PIL import Image, ImageQt, ImageEnhance
# from PyQt5.QtGui import Qt
from pyqtgraph.examples.text import text
from covid19gui_V3 import Ui_MainWindow
import os
import sys
input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
text_edit = QPlainTextEdit()
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
textWritten = QtCore.pyqtSignal(str)
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.ShowIButton.clicked.connect(self.do_test)
self.chooseStudy.clicked.connect(self.do_choosestudy)
self.RunButton_3.clicked.connect(self.do_runstudy)
self.scene = QtWidgets.QGraphicsScene(self)
self.graphicsView.setScene(self.scene)
w, h = input_img.size
self.pixmap_item = self.scene.addPixmap(QtGui.QPixmap())
# self.graphicsView.fitInView(QRectF(0, 0, w, h), Qt.KeepAspectRatio)
self.graphicsView.update()
self.plainTextEdit.update()
self.level = 1
self.enhancer = None
self.timer = QtCore.QTimer(interval=500, timeout=self.on_timeout)
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
def write(self, text):
self.textWritten.emit(str(text))
#QtCore.pyqtSlot()
def do_test(self):
# input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
self.enhancer = ImageEnhance.Brightness(input_img)
self.timer.start()
self.ShowIButton.setDisabled(True)
#QtCore.pyqtSlot()
def on_timeout(self):
if self.enhancer is not None:
result_img = self.enhancer.enhance(self.level)
qimage = ImageQt.ImageQt(result_img)
self.pixmap_item.setPixmap(QtGui.QPixmap.fromImage(qimage))
if self.level > 7:
self.timer.stop()
self.enhancer = None
self.level = 0
self.ShowIButton.setDisabled(False)
self.level = 1
self.ShowIButton.setDisabled(False)
#QtCore.pyqtSlot()
def do_choosestudy(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
if dlg.exec_():
filenames = dlg.selectedFiles()
f = open(filenames[0], 'r')
#QtCore.pyqtSlot()
def do_runstudy(self):
os.system("df -h")
# filetext = open('screenout.txt').read()
# filetext.close()
# textViewValue = self.plainTextEdit.toPlainText()
# QPlainTextEdit.appendPlainText(self, str(textViewValue))
# sys.stdout = self(textWritten=self.textWritten)
self.normalOutputWritten(text_edit)
def __del__(self):
# Restore sys.stdout
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text_edit):
#cursor = self.plainTextEdit.textCursor()
#cursor.movePosition(QtGui.QTextCursor.End)
#cursor.insertText(text_edit)
self.plainTextEdit.appendPlainText(text_edit)
#self.plainTextEdit.ensureCursorVisible()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
How can I make this work correctly?
Update 2
I indeed DID do research into the topic and this is one of the main resources I used to try to solve the issue before I posted my question: How to capture output of Python's interpreter and show in a Text widget?
Update 3
I have revised my code in the post to reflect code suggestions in the link I used to help me with my issue.
I am still unable to get this to run correctly. I now get this error:
self.plainTextEdit.appendPlainText(text_edit) TypeError:
appendPlainText(self, str): argument 1 has unexpected type
'QPlainTextEdit'
I have a user interface, TableManagerWindow, that I've been maintaining and developing in Qt designer. After converting via pyuic to a *.py file, I was able to implement what Ferdinand Beyer had suggested in the link you provided above. Simple button to print text to terminal and it indeed does get appended to the QTextEdit widget via append(). Not sure this fits the bill for you for some reason, but I can vouch that it worked for me as well. I'm not savvy enough to get the nuance that is causing your issue, but figured I'd put this here just in case. Admins feel free to delete this if it's extraneous, but it works.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# Define a stream, custom class, that reports data written to it, with a Qt signal
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class Ui_TableManagerWindow(object):
def setupUi(self, TableManagerWindow):
#define all of my widgets, layout, etc here
.
.
.
# Install a custom output stream by connecting sys.stdout to instance of EmmittingStream.
sys.stdout = EmittingStream(textWritten=self.output_terminal_written)
# Create my signal/connections for custom method
self.source_dir_button.clicked.connect(self.sourceDirButtonClicked)
self.retranslateUi(TableManagerWindow)
QtCore.QMetaObject.connectSlotsByName(TableManagerWindow)
def retranslateUi(self, TableManagerWindow):
.
.
.
#custom method that prints to output terminal. The point is to have this emmitted out to my QTextEdit widget.
def sourceDirButtonClicked(self):
for i in range(10):
print("The Source DIR button has been clicked " + str(i) + " times")
#custom method to write anything printed out to console/terminal to my QTextEdit widget via append function.
def output_terminal_written(self, text):
self.output_terminal_textEdit.append(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
TableManagerWindow = QtWidgets.QMainWindow()
ui = Ui_TableManagerWindow()
ui.setupUi(TableManagerWindow)
TableManagerWindow.show()
sys.exit(app.exec_())
I know you can set the alignment of the text using setAlignment(), but that has no effect on the placeholder text. Maybe you would need to edit the styleSheet of the underlying documentin order to do it? Or is the document only relevant for the actual text but not the placeholder?
Here's an MWE to play around with:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QApplication, QGridLayout, QTextEdit
class Test(QDialog):
def __init__(self):
super().__init__()
self.edit = QTextEdit()
self.edit.setPlaceholderText(
# '<html><body><p align="center">'
'this is a placeholder'
# '</p></body></html>'
)
self.edit.setAlignment(Qt.AlignHCenter)
self.lay = QGridLayout(self)
self.lay.addWidget(self.edit)
self.setLayout(self.lay)
if __name__ == '__main__':
app = QApplication(sys.argv)
GUI = Test()
GUI.show()
sys.exit(app.exec_())
The placeholder cannot be directly customized, as it is directly painted by the QTextEdit using the default top alignment, so the only solution is to subclass, overwrite the paintEvent and paint the placeholder using your own alignment.
You might also add much more control by using a QTextDocument, which will allow you to use html and custom colors/alignment/etc.
class HtmlPlaceholderTextEdit(QTextEdit):
_placeholderText = ''
def setPlaceholderText(self, text):
if Qt.mightBeRichText(text):
self._placeholderText = QTextDocument()
try:
color = self.palette().placeholderText().color()
except:
# for Qt < 5.12
color = self.palette().windowText().color()
color.setAlpha(128)
# IMPORTANT: the default stylesheet _MUST_ be set *before*
# adding any text, otherwise it won't be used.
self._placeholderText.setDefaultStyleSheet(
'''body {{color: rgba({}, {}, {}, {});}}
'''.format(*color.getRgb()))
self._placeholderText.setHtml(text)
else:
self._placeholderText = text
self.update()
def placeholderText(self):
return self._placeholderText
def paintEvent(self, event):
super().paintEvent(event)
if self.document().isEmpty() and self.placeholderText():
qp = QPainter(self.viewport())
margin = self.document().documentMargin()
r = QRectF(self.viewport().rect()).adjusted(margin, margin, -margin, -margin)
text = self.placeholderText()
if isinstance(text, str):
try:
color = self.palette().placeholderText().color()
except:
# for Qt < 5.12
color = self.palette().windowText().color()
color.setAlpha(128)
qp.setPen(color)
qp.drawText(r, self.alignment() | Qt.TextWordWrap, text)
else:
text.setPageSize(self.document().pageSize())
text.drawContents(qp, r)
class Test(QDialog):
def __init__(self):
super().__init__()
self.edit = HtmlPlaceholderTextEdit()
self.edit.setPlaceholderText(
'<html><body><p align="center">'
'this is a <font color="blue">placeholder</font>'
'</p></body></html>'
)
# ...
I am experiencing a rather strange issue with my PyQT QTextEdit.
When I enter a string from my QLineEdit it adds it but say I enter another the first string disappears I assume that's because I am not appending the text.
Any idea how I can do this?
Here is the relevant code:
self.mytext.setText(str(self.user) + ": " + str(self.line.text()) + "\n")
and the important one
self.mySignal.emit(self.decrypt_my_message(str(msg)).strip() + "\n")
Edit
I figured it out I needed to use a QTextCursor
self.cursor = QTextCursor(self.mytext.document())
self.cursor.insertText(str(self.user) + ": " + str(self.line.text()) + "\n")
The setText() method replaces all the current text, so you just need to use the append() method instead. (Note that both these methods automatically add a trailing newline).
import sys
from PyQt4 import QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton('Test')
self.edit = QtGui.QTextEdit()
layout.addWidget(self.edit)
layout.addWidget(self.button)
self.button.clicked.connect(self.handleTest)
def handleTest(self):
self.edit.append('spam: spam spam spam spam')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())