How to convert saved geometry to readable text?



  • When one calls saveGeometry() it stores the geometry information as a QByteArray. I would like to be able to print this to screen as a QString in order to debug an issue I have.

    I can't for the life of me work out what format Qt is saving this in and how to recover a readable string i.e. containing position / size of the widget in terms of readable ints.


  • Qt Champions 2016

    Hi this is the format

    QByteArray QWidget::saveGeometry() const
    {
        QByteArray array;
        QDataStream stream(&array, QIODevice::WriteOnly);
        stream.setVersion(QDataStream::Qt_4_0);
        const quint32 magicNumber = 0x1D9D0CB;
        // Version history:
        // - Qt 4.2 - 4.8.6, 5.0 - 5.3    : Version 1.0
        // - Qt 4.8.6 - today, 5.4 - today: Version 2.0, save screen width in addition to check for high DPI scaling.
        quint16 majorVersion = 2;
        quint16 minorVersion = 0;
        const int screenNumber = QApplication::desktop()->screenNumber(this);
        stream << magicNumber
               << majorVersion
               << minorVersion
               << frameGeometry()
               << normalGeometry()
               << qint32(screenNumber)
               << quint8(windowState() & Qt::WindowMaximized)
               << quint8(windowState() & Qt::WindowFullScreen)
               << qint32(QApplication::desktop()->screenGeometry(screenNumber).width()); // 1.1 onwards
        return array;
    }
    

    You can reuse the load function and simply convert the variables to strings using QString::number

    bool QWidget::restoreGeometry(const QByteArray &geometry)
    {
        if (geometry.size() < 4)
            return false;
        QDataStream stream(geometry);
        stream.setVersion(QDataStream::Qt_4_0);
    
        const quint32 magicNumber = 0x1D9D0CB;
        quint32 storedMagicNumber;
        stream >> storedMagicNumber;
        if (storedMagicNumber != magicNumber)
            return false;
    
        const quint16 currentMajorVersion = 2;
        quint16 majorVersion = 0;
        quint16 minorVersion = 0;
    
        stream >> majorVersion >> minorVersion;
    
        if (majorVersion > currentMajorVersion)
            return false;
        // (Allow all minor versions.)
    
    // these are the variables you want to output. 
        QRect restoredFrameGeometry;
         QRect restoredNormalGeometry;
        qint32 restoredScreenNumber;
        quint8 maximized;
        quint8 fullScreen;
        qint32 restoredScreenWidth = 0;
    
        stream >> restoredFrameGeometry
               >> restoredNormalGeometry
               >> restoredScreenNumber
               >> maximized
               >> fullScreen;
    
        if (majorVersion > 1)
            stream >> restoredScreenWidth;
    xxxx
    


  • @mrjj Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.