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. What should I use instead of QDesktopWidget::resized in Qt 6?
Forum Updated to NodeBB v4.3 + New Features

What should I use instead of QDesktopWidget::resized in Qt 6?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 646 Views 2 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.
  • G Offline
    G Offline
    Guy Gizmo
    wrote on 30 Nov 2022, 20:48 last edited by
    #1

    I'm in the process of converting a project from Qt 5 to Qt 6. I previously used the signal QDesktopWidget::resized to be notified of when anything about the current screen configuration changed, be it the resolution of one screen or the arrangement of multiple screens.

    However, QDesktopWidget has been removed from Qt 6, and I don't see anything that replaces this specific signal.

    While QScreen is obviously intended to be used as a replaced to QDesktopWidget, and I can use various signals of QScreen to be notified of the changes of one specific screen, there doesn't appear to be any signal that will notify me of changes to any screen, regardless of what that change is.

    There also doesn't seem to be an equivalent signal on QGuiApplication either.

    The only solution I can think of is to make use of the QGuiApplication::screenAdded signal, and every time a new screen is added, connect to its geometryChanged signal. But, quite frankly, that totally sucks. Not only is it more crufty code to do something that used to be a one liner, it also now means I'm going to have multiple signals fire every time something about the screen configuration changes, rather than just one. And I don't know how many signals will fire because any one configuration operation is not guaranteed to change every screen!

    Surely there's a better way. How do I do this in Qt 6?

    C 1 Reply Last reply 30 Nov 2022, 21:25
    0
    • G Guy Gizmo
      30 Nov 2022, 20:48

      I'm in the process of converting a project from Qt 5 to Qt 6. I previously used the signal QDesktopWidget::resized to be notified of when anything about the current screen configuration changed, be it the resolution of one screen or the arrangement of multiple screens.

      However, QDesktopWidget has been removed from Qt 6, and I don't see anything that replaces this specific signal.

      While QScreen is obviously intended to be used as a replaced to QDesktopWidget, and I can use various signals of QScreen to be notified of the changes of one specific screen, there doesn't appear to be any signal that will notify me of changes to any screen, regardless of what that change is.

      There also doesn't seem to be an equivalent signal on QGuiApplication either.

      The only solution I can think of is to make use of the QGuiApplication::screenAdded signal, and every time a new screen is added, connect to its geometryChanged signal. But, quite frankly, that totally sucks. Not only is it more crufty code to do something that used to be a one liner, it also now means I'm going to have multiple signals fire every time something about the screen configuration changes, rather than just one. And I don't know how many signals will fire because any one configuration operation is not guaranteed to change every screen!

      Surely there's a better way. How do I do this in Qt 6?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 Nov 2022, 21:25 last edited by
      #2

      @Guy-Gizmo said in What should I use instead of QDesktopWidget::resized in Qt 6?:

      Surely there's a better way. How do I do this in Qt 6?

      ok, no longer a one-liner but also no rocket-science:

      auto connectGeoChanged = [this](QScreen* s) {
          connect(s, &QScreen::geometryChanged, this, &Foo::onGeometryChanged);
      };
      connect(qApp, &QGuiApplication::screenAdded, this, connectGeoChanged);
      const auto screens = QGuiApplication::screens();
      for (auto s : screens) connectGeoChanged(s);
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Guy Gizmo
        wrote on 1 Dec 2022, 00:26 last edited by Guy Gizmo 12 Jan 2022, 00:28
        #3

        @Christian-Ehrlicher I do mention connecting to QGuiApplication::screenAdded in my original post and the trouble here is that, instead of getting one signal when the system screen configuration changes, I get multiple depending on how many screens are involved.

        I could of course use a QTimer that gets started on the first geometryChanged signal / call to onGeometryChanged(), reset on each subsequent signal, and so will fire when the geometryChanged signals from each screen have all finished. But this solution isn't great for a number of reasons:

        1. It's a race condition: I don't know long it will take for each screen signal to fire, and using too short of a timer could result in me erroneously processing the change to the system screen configuration multiple times. (Though practically speaking they'll probably all fire within a few milliseconds of each other, if not within the same millisecond.)
        2. My handler now is delayed however long it takes the timer to fire rather than handling it immediately, leaving a brief period of discontinuity where my app's internal representation of the current screen configuration is out-of-date. (Another race condition.)
        3. It's a whole lot of crufty code to do something that used to be a simple one liner

        If that's what it takes then I'll do it, and I can probably find a way to factor it into its own function or class to keep the cruftiness sequestered somewhere appropriate and out of my main code. But I'm really hoping there's a more elegant way of handling this, especially a method that avoids 1 and 2 above.

        That all said, it strikes me that the right way to handle this would be for QGuiApplication to have a signal that fires when the overall screen configuration changes, duplicating the functionality of QDesktopWidget::resized, since QGuiApplication already has several signals related to the system screen configuration. Maybe I'll file a feature request for that.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Dec 2022, 06:09 last edited by
          #4

          instead of getting one signal when the system screen configuration changes, I get multiple depending on how many screens are involved.

          You don't get one signal - you get one signal per change, see https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qdesktopwidget.cpp?h=5.15.2#n187

          My code does exactly the same as QDesktopWidget would do for a geometry change.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • G Offline
            G Offline
            Guy Gizmo
            wrote on 1 Dec 2022, 14:11 last edited by
            #5

            Well there it is, you are correct. I guess I'll just have to do that business with the timer and handle the race condition.

            1 Reply Last reply
            0

            1/5

            30 Nov 2022, 20:48

            • Login

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