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. get mainwindow size?
Forum Updated to NodeBB v4.3 + New Features

get mainwindow size?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.0k 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.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by JoeCFD
    #1

    All windows can be obtained from QGuiApplication::allWindows(). The main window can be found with a loop over all windows. Then its size can be obtained.

    Is there any easier way to get mainwindow size of a Qt app? I need it deep in my Qt app.

    JonBJ CP71C 2 Replies Last reply
    0
    • JoeCFDJ JoeCFD

      All windows can be obtained from QGuiApplication::allWindows(). The main window can be found with a loop over all windows. Then its size can be obtained.

      Is there any easier way to get mainwindow size of a Qt app? I need it deep in my Qt app.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @JoeCFD
      In a word, "No". If you need to access the main window (or any other window) from deep down in code you do need a helper function like you have found. You can make yours (maybe) a bit faster: I used QGuiApplication::topLevelWindows() for top-level only (no parent) as that will be what main window is, I don't know if Qt can produce that faster than allWindows().

      I found it fine, speed-wise. Of course if you do it a lot you could always cache the result fur re-use.

      I prefer this to saving some "globally accessible variable" which you set with the main window when you first create it. But of course that would be fast.

      JoeCFDJ 1 Reply Last reply
      2
      • JoeCFDJ JoeCFD

        All windows can be obtained from QGuiApplication::allWindows(). The main window can be found with a loop over all windows. Then its size can be obtained.

        Is there any easier way to get mainwindow size of a Qt app? I need it deep in my Qt app.

        CP71C Offline
        CP71C Offline
        CP71
        wrote on last edited by
        #3

        @JoeCFD
        Hi.
        I never did this, but a possible workaround is:

        In your virtual void QMainWindow::resizeEvent(QResizeEvent *event); you can store the size of application in variables where all your code can access them.

        It is only an idea! :)

        JoeCFDJ 1 Reply Last reply
        1
        • CP71C CP71

          @JoeCFD
          Hi.
          I never did this, but a possible workaround is:

          In your virtual void QMainWindow::resizeEvent(QResizeEvent *event); you can store the size of application in variables where all your code can access them.

          It is only an idea! :)

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @CP71 Unluckily global variables are not encouraged.

          CP71C 1 Reply Last reply
          2
          • JonBJ JonB

            @JoeCFD
            In a word, "No". If you need to access the main window (or any other window) from deep down in code you do need a helper function like you have found. You can make yours (maybe) a bit faster: I used QGuiApplication::topLevelWindows() for top-level only (no parent) as that will be what main window is, I don't know if Qt can produce that faster than allWindows().

            I found it fine, speed-wise. Of course if you do it a lot you could always cache the result fur re-use.

            I prefer this to saving some "globally accessible variable" which you set with the main window when you first create it. But of course that would be fast.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            @JonB Thanks for your reply. I tried both which are same in my app. No overhead issue. I expected to use one func call to get it. I will mark my post as solved.

            1 Reply Last reply
            1
            • JoeCFDJ JoeCFD

              @CP71 Unluckily global variables are not encouraged.

              CP71C Offline
              CP71C Offline
              CP71
              wrote on last edited by CP71
              #6

              @JoeCFD said in get mainwindow size?:

              Unluckily global variables are not encouraged.

              Correct!
              No global variables, somewhere like a core manager, view manager, data manager and so on. Objects that normally are visible quite everywhere. ;)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Out of curiosity, why do you need that information deep in your code ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                JoeCFDJ 1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  Out of curiosity, why do you need that information deep in your code ?

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #8

                  @SGaist I need to pop-up some dialogs and their sizes are scaled with mainwindow size. The sizes of dialogs and their children are set by professional UI designers.

                  1 Reply Last reply
                  0

                  • Login

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