I have a QGraphicsTextItem in which I editted the class to allow me to have the text displayed in the centre when a new line is formatted. The current issue I’m facing with it is that I can’t call the original class it will create another text element that isn’t centred. Also, I can’t use html styling to centring because I need to know when a new line is formatted and with the html option when it has reached its max width it creates a new line but you can’t retrieve the new line as a
(at leas I haven’t been able to), when using toHtml function.
I’ve tried setting the format of the text background when users highlight it but since I’m centring the text I only was able to get the highlighting working without centring the text. This is the code with the attempt to centring and with the highlight working:
import sys
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsTextItem, QStyle
from PyQt5.QtGui import QPainter, QPen, QColor, QFont, QTextCursor, QTextLayout, QAbstractTextDocumentLayout, QPalette, QTextCharFormat, QBrush, QTextOption
from PyQt5.QtCore import Qt, QRectF
class CenteredTextItem(QGraphicsTextItem):
def __init__(self, parent=None):
super().__init__(parent)
self.setTextInteractionFlags(Qt.TextEditorInteraction)
self.setFlags(QGraphicsTextItem.ItemIsSelectable | QGraphicsTextItem.ItemIsMovable | QGraphicsTextItem.ItemIsFocusable)
# Set text alignment to center
option = QTextOption()
option.setAlignment(Qt.AlignCenter)
self.document().setDefaultTextOption(option)
def paint(self, painter, option, widget):
painter.save()
painter.setFont(self.font())
bounding_rect = self.boundingRect()
# Reset formatting
cursor = QTextCursor(self.document())
format = QTextCharFormat()
format.setBackground(QBrush(Qt.transparent))
cursor.select(QTextCursor.Document)
cursor.mergeCharFormat(format)
# Apply highlight to selected text
if self.textCursor().hasSelection():
cursor = self.textCursor()
highlight_color = QColor(Qt.yellow) # Change this to your desired highlight color
format = QTextCharFormat()
format.setBackground(QBrush(highlight_color))
cursor.mergeCharFormat(format)
# Draw the document
self.document().drawContents(painter)
# Draw the bounding rectangle
painter.translate(-bounding_rect.topLeft())
painter.setPen(QPen(Qt.black, 1, Qt.SolidLine))
painter.drawRect(bounding_rect)
# Draw focus rectangle if item has focus
if option.state & QStyle.State_HasFocus:
pen = QPen(Qt.DashLine)
painter.setPen(pen)
painter.drawRect(bounding_rect)
painter.restore()
class MyGraphicsView(QGraphicsView):
def __init__(self):
super().__init__()
scene = QGraphicsScene(self)
self.setScene(scene)
self.setSceneRect(0, 0, 400, 300)
self.editable_text = CenteredTextItem()
self.editable_text.setPlainText('Centered Text ssssssssssssssssssssssssssssssssssssssssssssssssssssss test')
self.editable_text.setFont(QFont('Arial', 20))
scene.addItem(self.editable_text)
app = QApplication(sys.argv)
view = MyGraphicsView()
view.resize(800, 500)
view.show()
sys.exit(app.exec_())