QGraphicsTextItem RightToLeft text

885 views Asked by At

I have to write some Farsi texts to a QGraphicsTextItem , but I can't find how I can make QGraphicsTextItem to write RightToLeft!.
Can any one help me?.

I tryed this way but it didn't work for me:

class DiagramTextItem(QtGui.QGraphicsTextItem):

def __init__(self, parent=None, scene=None):
    super(DiagramTextItem, self).__init__(parent, scene)
    doc =QtGui.QTextDocument ('''شسشس
                              یییییگ''')
    txtOpt = QtGui.QTextOption()
    txtOpt.setAlignment(QtCore.Qt.AlignRight)
    doc.setDefaultTextOption(txtOpt)

Thank you.

1

There are 1 answers

2
ekhumoro On

It looks like you could use QTextOption.setTextDirection with Qt.RightToLeft.

But note that you may need to set the direction before you set the text:

class DiagramTextItem(QtGui.QGraphicsTextItem):
    def __init__(self, parent=None, scene=None):
        super(DiagramTextItem, self).__init__(parent, scene)
        doc = QtGui.QTextDocument()
        txtOpt = QtGui.QTextOption()
        txtOpt.setTextDirection(QtCore.Qt.RightToLeft)
        doc.setPlainText('''شسشس
                         یییییگ''')

If that doesn't work, try setting the cursor on the QGraphicsTextItem:

class DiagramTextItem(QtGui.QGraphicsTextItem):
    def __init__(self, parent=None, scene=None):
        super(DiagramTextItem, self).__init__(parent, scene)
        cursor = self.textCursor()
        format = cursor.charFormat()
        format.setLayoutDirection(QtCore.Qt.RightToLeft)
        cursor.setFormat(format)
        self.setTextCursor(cursor)
        self.setPlainText('''شسشس
                          یییییگ''')