Sophus
Sophus

Reputation: 491

PyQt 4: Get Position of Toolbar

Hy guys,

in my executable program there is a toolbar. Well, the user decides to move the toolbar. Now the toolbar is floating. I know I have to conntect the floating-signals that is emittted when the toolbar ist arranged by the user. How can I save the new position of the toolbar? I know the method of adding the toolbar to the main window with a position:self.addToolBar( Qt.LeftToolBarArea , toolbar_name). In the handle_floating()-method you see what I want: There I want to get the position currently, but how? You also see I have just added one member variable, named self.toolbar_pos, to hold the position of the toolbar. My idea is, when application is terminated I want to serialize this value to a file, and later, when application is ran again its will read that file and set the toolbar accordingly. But this is no problem. Currently I don't have no idea to get the position of the toolbar.

I need your help :)

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)

        self.toolbar_pos = None

        self.initUI()      

    def initUI(self):               

        exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(QtGui.qApp.quit)

        self.toolbar = QtGui.QToolBar(self)
        self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
        self.addToolBar(self.toolbar )
        self.toolbar.addAction(exitAction)

        self.toolbar.setAllowedAreas(QtCore.Qt.TopToolBarArea
                                     | QtCore.Qt.BottomToolBarArea
                                     | QtCore.Qt.LeftToolBarArea
                                     | QtCore.Qt.RightToolBarArea)

        self.addToolBar( QtCore.Qt.LeftToolBarArea , self.toolbar )

        self.toolbar.topLevelChanged.connect(self.handle_floating)


    def handle_floating(self, event):
        #   The topLevel parameter is true
        #   if the toolbar is now floating
        if not event:
            #   If the toolbar no longer floats,
            #   then calculate the position where the
            #   toolbar is located currently.
            self.toolbar_pos = None
            print "Get position: ?"



def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.setGeometry(300, 300, 300, 200)
    ex.setWindowTitle('Toolbar example')    
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Upvotes: 2

Views: 851

Answers (1)

ekhumoro
ekhumoro

Reputation: 120768

The QMainWindow class already has APIs for this: i.e. saveState and restoreState. These can be used to save and restore the state of all the toolbars and dock-widgets in your application.

To use them, you first need to make sure that all your toolbars and dock-widgets are given a unique object-name when they are created:

class Example(QtGui.QMainWindow):
    ...
    def initUI(self):               
        ...
        self.toolbar = QtGui.QToolBar(self)
        self.toolbar.setObjectName('foobar')

Then you can override closeEvent to save the state:

    class Example(QtGui.QMainWindow):
        ...
        def closeEvent(self, event):
            with open('/tmp/test.conf', 'wb') as stream:
                stream.write(self.saveState().data())

(NB: I've just used a temporary file here for testing, but it would obviously be much better to use something like QSettings in your real application).

Finally, you can restore the state that was saved previously:

class Example(QtGui.QMainWindow):    
    def __init__(self, parent=None):
        ...
        self.initUI()

        try:
            with open('/tmp/test.conf', 'rb') as stream:
                self.restoreState(QtCore.QByteArray(stream.read()))
        except IOError:
            pass

Upvotes: 2

Related Questions