Is there a simple way to restore widget sizes in Qt?
Question
I'm looking for a way to preserve the size of windows in a Qt app.
I've seen that there is the possibility of using the following method for every widget:
saveGeometry()
But really, I don't find this a satisfactioning method. Is there something like setAutosaveGeometry(True)
?
I'm especially looking for a way to store the widths of table columns.
Solution
The QHeaderView class also has two methods for saving and restoring it's state to and from a QByteArray: saveState()and restoreState()
A table view's headers are accessible via the horizontalHeader() and verticalHeader() methods.
OTHER TIPS
saveGeometry returns a QByteArray value, you need to store it somewhere.
Example:
void MainWindow::closeEvent(QCloseEvent *event){ QSettings settings; settings.setValue("geometry", saveGeometry()); }
For reading the geometry call the restoreGeometry function:
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent) { [...] QSettings settings; restoreGeometry(settings.value("geometry").toByteArray()); [...] }
To learn more about window geometry please read the documentation
See the Qt documentation on Restoring a Window's Geometry.