Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to convert saved geometry to readable text?
Forum Updated to NodeBB v4.3 + New Features

How to convert saved geometry to readable text?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oracle3001
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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
      
      O 1 Reply Last reply
      2
      • mrjjM mrjj

        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
        
        O Offline
        O Offline
        oracle3001
        wrote on last edited by
        #3

        @mrjj Thank you

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved