[PyQt] Storing a QFlags in a QSettings

Phil Thompson phil at riverbankcomputing.com
Tue May 26 15:15:02 BST 2009


On Tue, 26 May 2009 15:29:11 +0200, Jerome Vuarand
<jerome.vuarand at gmail.com> wrote:
> Hi,
> 
> I'm currently trying to modify an existing PyQt app so that it stores
> the windowState of its main window when it exits. The information is
> retrieved with the QWidget::windowState method, and restored with the
> QWidget::setWindowState method. The state itself it an object of type
> WindowStates, which is a QFlags.
> 
> When trying to save it with QSettings::setValue, I get the following
> errors. When I try:
> 
>     settings.setValue("windowstate", self.windowState())
> 
> I get:
> 
>     TypeError: argument 2 of QSettings.setValue() has an invalid type
> 
> When I try:
> 
>     settings.setValue("windowstate", QtCore.QVariant(self.windowState()))
> 
> I get:
> 
>     QVariant::save: unable to save type 262.
> 
> Finally I realized there is a __int__ method in the WindowStates
> object, which I guess is mapped to the C++ "operator int" method.
> However, while I can save that value, I cannot rebuild the
> WindowStates object from an int. There is no constructor taking an int
> as parameter in QFlags.
> 
> So what is the correct way to save and restore a windowState ?
> 
> Note that I'm a noob at both Qt and Python.

Good question - I'll add a QFlags(int) ctor.

In the meantime I think you will need to convert each bit to the
corresponding enum and or with an initially empty QFlags().

Phil


More information about the PyQt mailing list