Issue
I'm trying to make a config file that would contain information about window size, position and state when closing. When opening again it should recover based on that info.
So I'm using closeEvent
to trigger this function:
cfg = ConfigParser() # create config object
def saveConfig(gui):
winW = gui.centralwidget.frameGeometry().width() # width of window
winH = gui.centralwidget.frameGeometry().height() # height of window
cfg['window'] = {} # window settings
window = cfg['window']
window['size'] = f"{winW},{winH}" # save current window size
window['pos'] = f"{gui.x()},{gui.y()}" # save current window position
with open(CONFIG_PATH, 'w') as file: # write config to file
cfg.write(file)
Here's the loadConfig function:
def loadConfig(gui):
cfg.read(CONFIG_PATH) # read config from CONFIG_PATH
if 'window' in cfg: # window settings
window = cfg['window']
size = window.get('size').split(',')
pos = window.get('pos').split(',')
gui.resize(int(size[0]), int(size[1])) # resize window to saved size
gui.move(int(pos[0]), int(pos[1])) # move window to save position
else:
gui.resize(1110,783)
gui.move(398,94)
However, if the window is closed while it's maximalized, it doesn't appear maximalized again after launch. Just in the same size and position.
I read that you can get windowState(). Then it would be easy to compare it if it's Qt.WindowMaximized
but when I use print(gui.centralwidget.windowState()
it returns <PyQt5.QtCore.Qt.WindowStates object at 0x000001E7B1DBF970>
.
Any ideas?
Solution
All Qt enums and flags are integer or bitwise values, so they can be easily converted into "readable" format:
state = int(self.windowState())
The flags can be also directly compared even without knowing the numeric value, using bitwise operators:
isMaximized = self.windowState() & QtCore.Qt.WindowMaximized
Note that you can just call the related convenience functions if you want to know a specific state: isMaximized()
, isMinimized()
and isFullScreen()
.
Finally, remember that Qt already provides saveGeometry()
and restoreGeometry()
, which not only stores the geometry of the window, but also the screen number and geometry on which the widget currently is, which ensures that the window will be restored as much as possible as it was when the state was saved, while ensuring it's always visible anyway, even if the screen setup has changed.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.