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. The function window()->windowHandle(); always returns nullptr in my custom widget.
Forum Updated to NodeBB v4.3 + New Features

The function window()->windowHandle(); always returns nullptr in my custom widget.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 360 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.
  • P Offline
    P Offline
    Paul Xu
    wrote on last edited by
    #1

    I want to obtain the QWindow handler within the constructor of my custom widget, but it always returns a null pointer.

    class MyWidget : public QWidget {
        Q_OBJECT
    
     public:
        MyWidget() {
               QWindow *win = window()->windowHandle();
               std::println("win equals nullptr: {}", win == nullptr);  // prints true
        }
    };
    

    How can I get the QWindow handler ?

    Pl45m4P 1 Reply Last reply
    0
    • P Paul Xu

      I want to obtain the QWindow handler within the constructor of my custom widget, but it always returns a null pointer.

      class MyWidget : public QWidget {
          Q_OBJECT
      
       public:
          MyWidget() {
                 QWindow *win = window()->windowHandle();
                 std::println("win equals nullptr: {}", win == nullptr);  // prints true
          }
      };
      

      How can I get the QWindow handler ?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #3

      @Paul-Xu

      AFAIK the widget has no valid window handle while being constructed.
      Try after the widget is shown on screen.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2
      • hskoglundH Online
        hskoglundH Online
        hskoglund
        wrote on last edited by
        #2

        The QWindows has not been rendered yet, try holding your horses for say 20 ms:

        ...
           MyWidget() {
              QTimer::singleShot(20,[this] {
                   QWindow *win = window()->windowHandle();
                   std::println("win equals nullptr: {}", win == nullptr);  // prints true
              });
           }
        
        1 Reply Last reply
        1
        • P Paul Xu

          I want to obtain the QWindow handler within the constructor of my custom widget, but it always returns a null pointer.

          class MyWidget : public QWidget {
              Q_OBJECT
          
           public:
              MyWidget() {
                     QWindow *win = window()->windowHandle();
                     std::println("win equals nullptr: {}", win == nullptr);  // prints true
              }
          };
          

          How can I get the QWindow handler ?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #3

          @Paul-Xu

          AFAIK the widget has no valid window handle while being constructed.
          Try after the widget is shown on screen.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2
          • P Offline
            P Offline
            Paul Xu
            wrote on last edited by Paul Xu
            #4

            @Pl45m4

            void MyWidget::showEvent(QShowEvent *event) {
                if(this->win == nullptr) {
                       this->win = window()->windowHandle();
               }
               QWidget::showEvent(event);
            }
            

            Apart from this way, I wonder if there are any other more elegant solutions?

            JonBJ 1 Reply Last reply
            0
            • P Paul Xu

              @Pl45m4

              void MyWidget::showEvent(QShowEvent *event) {
                  if(this->win == nullptr) {
                         this->win = window()->windowHandle();
                 }
                 QWidget::showEvent(event);
              }
              

              Apart from this way, I wonder if there are any other more elegant solutions?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #5

              @Paul-Xu
              If it works (I would have put it after the QWidget::showEvent(event);) then it's fine, what is not "elegant" about it?

              1 Reply Last reply
              0
              • P Paul Xu has marked this topic as solved on

              • Login

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