Results 1 to 9 of 9

Thread: Always Use RTL in QPlainTextEdit for PyQt4

  1. #1
    Join Date
    Aug 2015
    Posts
    8
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Always Use RTL in QPlainTextEdit for PyQt4

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    Quote Originally Posted by leomoon View Post
    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

    Quote Originally Posted by leomoon View Post
    2- How to change QPlainTextEdit font using QFontComboBox.
    By changing the default font of the edit's text document.

    Quote Originally Posted by leomoon View Post
    3- How to change QPlainTextEdit font point size using QSpinBox.
    By changing the default font of the edit's text document.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    8
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    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!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    Quote Originally Posted by leomoon View Post
    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,
    _

  5. #5
    Join Date
    Aug 2015
    Posts
    8
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    This is a simple example:
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'test.ui'
    4. #
    5. # Created by: PyQt4 UI code generator 4.11.4
    6. #
    7. # WARNING! All changes made in this file will be lost!
    8.  
    9. from PyQt4 import QtCore, QtGui
    10.  
    11. try:
    12. _fromUtf8 = QtCore.QString.fromUtf8
    13. except AttributeError:
    14. def _fromUtf8(s):
    15. return s
    16.  
    17. try:
    18. _encoding = QtGui.QApplication.UnicodeUTF8
    19. def _translate(context, text, disambig):
    20. return QtGui.QApplication.translate(context, text, disambig, _encoding)
    21. except AttributeError:
    22. def _translate(context, text, disambig):
    23. return QtGui.QApplication.translate(context, text, disambig)
    24.  
    25. class Ui_MainWindow(object):
    26. def setupUi(self, MainWindow):
    27. MainWindow.setObjectName(_fromUtf8("MainWindow"))
    28. MainWindow.resize(640, 480)
    29. self.centralwidget = QtGui.QWidget(MainWindow)
    30. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    31. self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
    32. self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
    33. self.plainTextEdit = QtGui.QPlainTextEdit(self.centralwidget)
    34. self.plainTextEdit.setLayoutDirection(QtCore.Qt.RightToLeft)
    35. self.plainTextEdit.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    36. self.plainTextEdit.setObjectName(_fromUtf8("plainTextEdit"))
    37. self.horizontalLayout.addWidget(self.plainTextEdit)
    38. MainWindow.setCentralWidget(self.centralwidget)
    39.  
    40. self.retranslateUi(MainWindow)
    41. QtCore.QMetaObject.connectSlotsByName(MainWindow)
    42.  
    43. def retranslateUi(self, MainWindow):
    44. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    45.  
    46.  
    47. if __name__ == "__main__":
    48. import sys
    49. app = QtGui.QApplication(sys.argv)
    50. MainWindow = QtGui.QMainWindow()
    51. ui = Ui_MainWindow()
    52. ui.setupUi(MainWindow)
    53. MainWindow.show()
    54. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Probably I'm missing something really simple!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    Quote Originally Posted by leomoon View Post
    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,
    _

  7. #7
    Join Date
    Aug 2015
    Posts
    8
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    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,
    _

  9. #9
    Join Date
    Aug 2015
    Posts
    8
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Always Use RTL in QPlainTextEdit for PyQt4

    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.
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'toggleDir.ui'
    4. #
    5. # Created by: PyQt4 UI code generator 4.11.4
    6. #
    7. # WARNING! All changes made in this file will be lost!
    8.  
    9. from PyQt4 import QtCore, QtGui
    10.  
    11. dir = 'rtl'
    12.  
    13. try:
    14. _fromUtf8 = QtCore.QString.fromUtf8
    15. except AttributeError:
    16. def _fromUtf8(s):
    17. return s
    18.  
    19. try:
    20. _encoding = QtGui.QApplication.UnicodeUTF8
    21. def _translate(context, text, disambig):
    22. return QtGui.QApplication.translate(context, text, disambig, _encoding)
    23. except AttributeError:
    24. def _translate(context, text, disambig):
    25. return QtGui.QApplication.translate(context, text, disambig)
    26.  
    27. class Ui_MainWindow(object):
    28. def setupUi(self, MainWindow):
    29. MainWindow.setObjectName(_fromUtf8("MainWindow"))
    30. MainWindow.resize(350, 350)
    31. MainWindow.setMinimumSize(QtCore.QSize(350, 350))
    32. MainWindow.setMaximumSize(QtCore.QSize(350, 350))
    33. self.centralwidget = QtGui.QWidget(MainWindow)
    34. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    35. self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
    36. self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
    37. self.horizontalLayout = QtGui.QHBoxLayout()
    38. self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
    39. self.fontName = QtGui.QFontComboBox(self.centralwidget)
    40. self.fontName.setObjectName(_fromUtf8("fontName"))
    41. self.horizontalLayout.addWidget(self.fontName)
    42. self.fontSize = QtGui.QSpinBox(self.centralwidget)
    43. self.fontSize.setProperty("value", 11)
    44. self.fontSize.setObjectName(_fromUtf8("fontSize"))
    45. self.horizontalLayout.addWidget(self.fontSize)
    46. self.verticalLayout.addLayout(self.horizontalLayout)
    47. self.edit = QtGui.QTextEdit(self.centralwidget)
    48. self.edit.setObjectName(_fromUtf8("edit"))
    49. self.edit.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    50. self.verticalLayout.addWidget(self.edit)
    51. self.toggleDirBtn = QtGui.QPushButton(self.centralwidget)
    52. font = QtGui.QFont()
    53. font.setPointSize(15)
    54. self.toggleDirBtn.setFont(font)
    55. self.toggleDirBtn.setObjectName(_fromUtf8("toggleDirBtn"))
    56. self.verticalLayout.addWidget(self.toggleDirBtn)
    57. MainWindow.setCentralWidget(self.centralwidget)
    58. option=self.edit.document().defaultTextOption()
    59. option.setTextDirection(QtCore.Qt.RightToLeft)
    60. self.edit.document().setDefaultTextOption(option)
    61. self.edit.setLayoutDirection(QtCore.Qt.RightToLeft) #switch scrollerbar
    62. self.edit.setTabStopWidth(33) #set tab size for editor
    63. self.fontName.currentFontChanged.connect(self._fontName)
    64. self.fontSize.valueChanged.connect(self._fontSize)
    65. self.toggleDirBtn.clicked.connect(self._toggleDir)
    66.  
    67. self.retranslateUi(MainWindow)
    68. QtCore.QMetaObject.connectSlotsByName(MainWindow)
    69.  
    70. def _fontName(self):
    71. font = QtGui.QFont()
    72. font.setFamily(self.fontName.currentText())
    73. font.setPointSize(int(self.fontSize.value()))
    74. self.edit.setFont(font)
    75. self.edit.setFocus()
    76.  
    77. def _fontSize(self):
    78. font = QtGui.QFont()
    79. font.setFamily(self.fontName.currentText())
    80. font.setPointSize(int(self.fontSize.value()))
    81. self.edit.setFont(font)
    82. self.edit.setFocus()
    83.  
    84. def _toggleDir(self):
    85. global dir
    86. if dir == 'rtl':
    87. option=self.edit.document().defaultTextOption()
    88. option.setTextDirection(QtCore.Qt.LeftToRight)
    89. self.edit.document().setDefaultTextOption(option)
    90. self.edit.setLayoutDirection(QtCore.Qt.LeftToRight) #switch scrollerbar
    91. dir = 'ltr'
    92. else:
    93. option=self.edit.document().defaultTextOption()
    94. option.setTextDirection(QtCore.Qt.RightToLeft)
    95. self.edit.document().setDefaultTextOption(option)
    96. self.edit.setLayoutDirection(QtCore.Qt.RightToLeft) #switch scrollerbar
    97. dir = 'rtl'
    98.  
    99. def retranslateUi(self, MainWindow):
    100. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    101. self.toggleDirBtn.setText(_translate("MainWindow", "Toggle Direction", None))
    102.  
    103.  
    104. if __name__ == "__main__":
    105. import sys
    106. app = QtGui.QApplication(sys.argv)
    107. MainWindow = QtGui.QMainWindow()
    108. ui = Ui_MainWindow()
    109. ui.setupUi(MainWindow)
    110. MainWindow.show()
    111. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 5th August 2012, 01:03
  2. [PyQt4] PyQt4 + gcin issue
    By fieliapm in forum Installation and Deployment
    Replies: 0
    Last Post: 28th September 2010, 08:04
  3. PyQt4 - QPlainTextEdit: performance when hidding blocks
    By josemaria.alkala in forum Newbie
    Replies: 6
    Last Post: 27th June 2010, 14:58
  4. QPlainTextEdit
    By deeee in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2010, 01:05
  5. QPlainTextEdit
    By Carlsberg in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2009, 06:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.