Search code examples
pythonpyqtpyqt5qmainwindowqtoolbar

How to change default position of toolbar?


I'm using PyQt5, QMainWindow and I want to change default position of toolbar to the right. How can I do it?

User can carry toolbar to the edges of the window using mouse, but how can I do it using program?

def initUI(self):
    self.toolbar = self.addToolBar('Example')

Solution

  • You have to use the addToolBar method as shown below:

    import sys
    from PyQt5 import QtCore, QtWidgets
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            self.toolbar = QtWidgets.QToolBar("Example")
            self.addToolBar(QtCore.Qt.RightToolBarArea, self.toolbar)
    
            self.toolbar.addAction("action 1")
            self.toolbar.addAction("action 2")
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.resize(640, 480)
        w.show()
        sys.exit(app.exec_())