Friday, April 15, 2011

Is there a simple way to restore widget sizes in Qt?

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.

From stackoverflow
  • 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

    Georg : I knew that already. I'm looking for a way to restore the layout of a QTableView, not only the window geometry.
  • See the Qt documentation on Restoring a Window's Geometry.

    Georg : This doesn't store the geometry of the children of the window. (Not all of them should be stored though…)
  • 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.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.