QGraphicsTextItem Centring Issues

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_())

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật