Search code examples
pythonpyqtpyqt4right-to-left

Right-to-left alignment of PyQt4 menus


I use PyQt4 for designing GUI.

I want to know is there a way to sort menu items from right to left instead left to right for languages such as Arabic or Persian?


Solution

  • You have to use the setLayoutDirection() method of QApplicacion to indicate the Qt.RightToLeft address as shown below:

    class menudemo(QMainWindow):
       def __init__(self, parent = None):
          super(menudemo, self).__init__(parent)
    
          layout = QHBoxLayout()
          bar = self.menuBar()
          file = bar.addMenu("ملف")
          file.addAction("الجديد")
    
          save = QAction("حفظ",self)
          file.addAction(save)
    
          edit = file.addMenu("تصحيح")
          edit.addAction("نسخ")
          edit.addAction("معجون")
    
          quit = QAction("استقال",self) 
          file.addAction(quit)
          file.triggered[QAction].connect(self.processtrigger)
          self.setLayout(layout)
          self.setWindowTitle("RTL")
    
       def processtrigger(self,q):
          print(q.text()+" is triggered")
    
    if __name__ == '__main__':
       app = QApplication(sys.argv)
       app.setLayoutDirection(Qt.RightToLeft)
       ex = menudemo()
       ex.show()
       sys.exit(app.exec_())
    

    Screenshot:

    enter image description here