PDA

View Full Version : Always Use RTL in QPlainTextEdit for PyQt4



leomoon
8th August 2015, 23:59
Hello,

I'm very new to PyQt and I have 3 simple problem. But I can't make it work.

1- I want the QPlainTextEdit to stay RTL (with text-align right) at ALL TIMES unless I tell it to go LTR. Doesn't matter what kind of text is being entered.
2- How to change QPlainTextEdit font using QFontComboBox.
3- How to change QPlainTextEdit font point size using QSpinBox.

Thanks for your help.

anda_skoa
9th August 2015, 10:17
1- I want the QPlainTextEdit to stay RTL (with text-align right) at ALL TIMES unless I tell it to go LTR. Doesn't matter what kind of text is being entered.

Probably by setting the layout direction on the defaultTextOption property of the edit's text document


2- How to change QPlainTextEdit font using QFontComboBox.

By changing the default font of the edit's text document.


3- How to change QPlainTextEdit font point size using QSpinBox.

By changing the default font of the edit's text document.

Cheers,
_

leomoon
9th August 2015, 23:58
Yea, I've tried that. That just puts the scrollerbar to the left! And I tried to align text to right, no luck there either!
self.editor.setAlignment(QtCore.Qt.AlignRight)
self.editor.setAlignment(Qt.AlignLeft)

Both say QPlainTextEdit doesn't have setAlignment attribute!

anda_skoa
10th August 2015, 06:26
Yea, I've tried that.
That just puts the scrollerbar to the left!

Can you show that code?
It sounds a bit like you set the layout direction on the editor itself.

Cheers,
_

leomoon
10th August 2015, 06:34
This is a simple example:


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(640, 480)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.plainTextEdit = QtGui.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setLayoutDirection(QtCore.Qt.Ri ghtToLeft)
self.plainTextEdit.setVerticalScrollBarPolicy(QtCo re.Qt.ScrollBarAlwaysOn)
self.plainTextEdit.setObjectName(_fromUtf8("plainTextEdit"))
self.horizontalLayout.addWidget(self.plainTextEdit )
MainWindow.setCentralWidget(self.centralwidget)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())


Probably I'm missing something really simple!

anda_skoa
10th August 2015, 15:47
Probably I'm missing something really simple!

Yes, you are missing that I wrote "the edit's text document" :-)

The text widgets operate on a QTextDocument instance, which handles all the text processing.
It has properties for default font and default text options, one of which is the text's layout direction.

Cheers,
_

leomoon
12th August 2015, 11:36
Dude,

I'm lost. I don't know how to use a QTextDocument to edit the editor's property. I keep getting error. And I can't find any example anywhere about forcing RTL.

anda_skoa
12th August 2015, 12:22
I have no idea what you are talking about, it certainly has nothing to do with what I suggested earlier.

Get the text document from the text edit, change the document's properties.

Cheers,
_

leomoon
14th August 2015, 01:16
I made an example if anybody else had the same problem. This example changes the font size, font name and toggles direction on the QTextEdit. font name and font size will be changed on the whole editor and toggling direction is not same as align left and right.


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'toggleDir.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

dir = 'rtl'

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(350, 350)
MainWindow.setMinimumSize(QtCore.QSize(350, 350))
MainWindow.setMaximumSize(QtCore.QSize(350, 350))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.fontName = QtGui.QFontComboBox(self.centralwidget)
self.fontName.setObjectName(_fromUtf8("fontName"))
self.horizontalLayout.addWidget(self.fontName)
self.fontSize = QtGui.QSpinBox(self.centralwidget)
self.fontSize.setProperty("value", 11)
self.fontSize.setObjectName(_fromUtf8("fontSize"))
self.horizontalLayout.addWidget(self.fontSize)
self.verticalLayout.addLayout(self.horizontalLayou t)
self.edit = QtGui.QTextEdit(self.centralwidget)
self.edit.setObjectName(_fromUtf8("edit"))
self.edit.setVerticalScrollBarPolicy(QtCore.Qt.Scr ollBarAlwaysOn)
self.verticalLayout.addWidget(self.edit)
self.toggleDirBtn = QtGui.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(15)
self.toggleDirBtn.setFont(font)
self.toggleDirBtn.setObjectName(_fromUtf8("toggleDirBtn"))
self.verticalLayout.addWidget(self.toggleDirBtn)
MainWindow.setCentralWidget(self.centralwidget)
option=self.edit.document().defaultTextOption()
option.setTextDirection(QtCore.Qt.RightToLeft)
self.edit.document().setDefaultTextOption(option)
self.edit.setLayoutDirection(QtCore.Qt.RightToLeft ) #switch scrollerbar
self.edit.setTabStopWidth(33) #set tab size for editor
self.fontName.currentFontChanged.connect(self._fon tName)
self.fontSize.valueChanged.connect(self._fontSize)
self.toggleDirBtn.clicked.connect(self._toggleDir)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def _fontName(self):
font = QtGui.QFont()
font.setFamily(self.fontName.currentText())
font.setPointSize(int(self.fontSize.value()))
self.edit.setFont(font)
self.edit.setFocus()

def _fontSize(self):
font = QtGui.QFont()
font.setFamily(self.fontName.currentText())
font.setPointSize(int(self.fontSize.value()))
self.edit.setFont(font)
self.edit.setFocus()

def _toggleDir(self):
global dir
if dir == 'rtl':
option=self.edit.document().defaultTextOption()
option.setTextDirection(QtCore.Qt.LeftToRight)
self.edit.document().setDefaultTextOption(option)
self.edit.setLayoutDirection(QtCore.Qt.LeftToRight ) #switch scrollerbar
dir = 'ltr'
else:
option=self.edit.document().defaultTextOption()
option.setTextDirection(QtCore.Qt.RightToLeft)
self.edit.document().setDefaultTextOption(option)
self.edit.setLayoutDirection(QtCore.Qt.RightToLeft ) #switch scrollerbar
dir = 'rtl'

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.toggleDirBtn.setText(_translate("MainWindow", "Toggle Direction", None))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())