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. QWidget top level widget
Forum Updated to NodeBB v4.3 + New Features

QWidget top level widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 5 Posters 2.2k Views 3 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.
  • A Offline
    A Offline
    Alexey Serebryakov
    wrote on 1 Oct 2022, 06:16 last edited by Alexey Serebryakov 10 Jan 2022, 06:42
    #1

    Hi guys, how can I create a top level widgets?

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        
        QWidget w1; // is it top level widget?
        QWidget w2; // is it top level widget?
    
        w1.show();
        QString ptrStr1 = QString("0x%1").arg((quintptr)w1.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
        QMessageBox::information(&w1, "Info1", ptrStr1, QMessageBox::StandardButton::Ok); // ok, correct window handle
    
        w2.show();    
        QString ptrStr2 = QString("0x%1").arg((quintptr)w2.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
        QMessageBox::information(&w2, "Info2", ptrStr2, QMessageBox::StandardButton::Ok); // but 0x00000000
    
        return a.exec();
    }
    

    Whats wrong?
    Thanks a lot.

    J 1 Reply Last reply 1 Oct 2022, 08:21
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Oct 2022, 06:26 last edited by mrjj 10 Jan 2022, 06:26
      #2

      Hi
      If you don't assign a parent to a QWidget it becomes top level. (a window)
      You can also use setWiundowsFlags for the same effect
      https://doc.qt.io/qt-6/qtwidgets-widgets-windowflags-example.html

      Im surprised that w2 is zero. Does that happen every time?

      what if you swap w1 and w2 processing ?

          w2.show();    
          QString ptrStr2 = QString("0x%1").arg((quintptr)w2.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
          QMessageBox::information(&w2, "Info1", ptrStr2, QMessageBox::StandardButton::Ok); 
      
         w1.show();
          QString ptrStr1 = QString("0x%1").arg((quintptr)w1.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
          QMessageBox::information(&w1, "Info", ptrStr1, QMessageBox::StandardButton::Ok);
      
      
      
      A 1 Reply Last reply 1 Oct 2022, 06:57
      0
      • M mrjj
        1 Oct 2022, 06:26

        Hi
        If you don't assign a parent to a QWidget it becomes top level. (a window)
        You can also use setWiundowsFlags for the same effect
        https://doc.qt.io/qt-6/qtwidgets-widgets-windowflags-example.html

        Im surprised that w2 is zero. Does that happen every time?

        what if you swap w1 and w2 processing ?

            w2.show();    
            QString ptrStr2 = QString("0x%1").arg((quintptr)w2.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
            QMessageBox::information(&w2, "Info1", ptrStr2, QMessageBox::StandardButton::Ok); 
        
           w1.show();
            QString ptrStr1 = QString("0x%1").arg((quintptr)w1.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
            QMessageBox::information(&w1, "Info", ptrStr1, QMessageBox::StandardButton::Ok);
        
        
        
        A Offline
        A Offline
        Alexey Serebryakov
        wrote on 1 Oct 2022, 06:57 last edited by Alexey Serebryakov 10 Jan 2022, 06:57
        #3

        @mrjj yes every time.

        Oh, but if I swap w1 and w2 processing there were correct handles. :-) Why that heppened?

        M 1 Reply Last reply 1 Oct 2022, 06:59
        0
        • A Alexey Serebryakov
          1 Oct 2022, 06:57

          @mrjj yes every time.

          Oh, but if I swap w1 and w2 processing there were correct handles. :-) Why that heppened?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Oct 2022, 06:59 last edited by
          #4

          @Alexey-Serebryakov
          ehh so swapping them, then both works? (o.O)

          Well, I wonder if it's due to we do it before a.exec(); (before ever shown)

          if you add a button and do it in its clicked slot, we still have one fail or what happens ?

          A 1 Reply Last reply 2 Oct 2022, 03:49
          1
          • A Alexey Serebryakov
            1 Oct 2022, 06:16

            Hi guys, how can I create a top level widgets?

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                
                QWidget w1; // is it top level widget?
                QWidget w2; // is it top level widget?
            
                w1.show();
                QString ptrStr1 = QString("0x%1").arg((quintptr)w1.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
                QMessageBox::information(&w1, "Info1", ptrStr1, QMessageBox::StandardButton::Ok); // ok, correct window handle
            
                w2.show();    
                QString ptrStr2 = QString("0x%1").arg((quintptr)w2.windowHandle(), QT_POINTER_SIZE * 2, 16, QChar('0'));
                QMessageBox::information(&w2, "Info2", ptrStr2, QMessageBox::StandardButton::Ok); // but 0x00000000
            
                return a.exec();
            }
            

            Whats wrong?
            Thanks a lot.

            J Offline
            J Offline
            JonB
            wrote on 1 Oct 2022, 08:21 last edited by
            #5

            @Alexey-Serebryakov said in QWidget top level widget:

            // ok, correct window handle

            How would you even know whether it's "correct"? :) Maybe all you mean is it's non-zero?

            Since the code variants with the order swapped are functionally equivalent (the compiled code may well actually beidentical), I would guess no native window handle is actually assigned until the widgets are physically shown during the a.exec(), as @mrjj suggests.

            1 Reply Last reply
            1
            • M mrjj
              1 Oct 2022, 06:59

              @Alexey-Serebryakov
              ehh so swapping them, then both works? (o.O)

              Well, I wonder if it's due to we do it before a.exec(); (before ever shown)

              if you add a button and do it in its clicked slot, we still have one fail or what happens ?

              A Offline
              A Offline
              Alexey Serebryakov
              wrote on 2 Oct 2022, 03:49 last edited by
              #6

              @mrjj is it correct code?

              class Widget : public QWidget
              {
                  Q_OBJECT
              public:
                   Widget(QWidget *parent = nullptr) : QWidget(parent, Qt::Window) {}
                  ~Widget();
              };
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  Widget w1;
                  Widget w2;
              
                  w1.show();
                  w1.windowHandle()->setScreen(QApplication::screens()[0]);
              
                  w2.show();
                  w2.windowHandle()->setScreen(QApplication::screens()[1]);
              
                  return a.exec();
              }
              
              M Z 2 Replies Last reply 2 Oct 2022, 07:12
              1
              • A Alexey Serebryakov
                2 Oct 2022, 03:49

                @mrjj is it correct code?

                class Widget : public QWidget
                {
                    Q_OBJECT
                public:
                     Widget(QWidget *parent = nullptr) : QWidget(parent, Qt::Window) {}
                    ~Widget();
                };
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    Widget w1;
                    Widget w2;
                
                    w1.show();
                    w1.windowHandle()->setScreen(QApplication::screens()[0]);
                
                    w2.show();
                    w2.windowHandle()->setScreen(QApplication::screens()[1]);
                
                    return a.exec();
                }
                
                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Oct 2022, 07:12 last edited by
                #7

                Hi
                Yes its correct
                as such but windowHandle() can return null so could crash.

                Also note with setScreen that on some systems with not work as one expect as
                the monitors are group by the OS as one big screen.

                A 1 Reply Last reply 2 Oct 2022, 07:21
                0
                • M mrjj
                  2 Oct 2022, 07:12

                  Hi
                  Yes its correct
                  as such but windowHandle() can return null so could crash.

                  Also note with setScreen that on some systems with not work as one expect as
                  the monitors are group by the OS as one big screen.

                  A Offline
                  A Offline
                  Alexey Serebryakov
                  wrote on 2 Oct 2022, 07:21 last edited by Alexey Serebryakov 10 Feb 2022, 07:21
                  #8

                  @mrjj For the top level widget windowHandle() must be valid always.

                  M 1 Reply Last reply 2 Oct 2022, 07:32
                  0
                  • A Alexey Serebryakov
                    2 Oct 2022, 07:21

                    @mrjj For the top level widget windowHandle() must be valid always.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 2 Oct 2022, 07:32 last edited by
                    #9

                    @Alexey-Serebryakov

                    Yes they should and hopefully not null even the widget was not shown yet.

                    So you play around with a multi screen setup ?

                    A 1 Reply Last reply 2 Oct 2022, 10:12
                    0
                    • M mrjj
                      2 Oct 2022, 07:32

                      @Alexey-Serebryakov

                      Yes they should and hopefully not null even the widget was not shown yet.

                      So you play around with a multi screen setup ?

                      A Offline
                      A Offline
                      Alexey Serebryakov
                      wrote on 2 Oct 2022, 10:12 last edited by Alexey Serebryakov 10 Feb 2022, 10:17
                      #10

                      @mrjj yep.
                      So, I have Qt-widget based application. Also I have notification widget which must be pop up on the selected screen/monitor in right bottom corner. Like Windows notification center or how in MS Outlook.
                      User can select the screen which display that popup widget.
                      Idially I need something like this:

                      MyPopupWidget::show(QScreen *screen) {
                        show();
                        windowHandle()->setScreen(screen);
                      }
                      
                      M 1 Reply Last reply 2 Oct 2022, 10:20
                      0
                      • A Alexey Serebryakov
                        2 Oct 2022, 10:12

                        @mrjj yep.
                        So, I have Qt-widget based application. Also I have notification widget which must be pop up on the selected screen/monitor in right bottom corner. Like Windows notification center or how in MS Outlook.
                        User can select the screen which display that popup widget.
                        Idially I need something like this:

                        MyPopupWidget::show(QScreen *screen) {
                          show();
                          windowHandle()->setScreen(screen);
                        }
                        
                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 2 Oct 2022, 10:20 last edited by mrjj 10 Feb 2022, 10:21
                        #11

                        @Alexey-Serebryakov
                        Ohh.
                        Like Win 10 Toast messages?

                        But you must run on other platforms and prefer your own ?

                        https://doc.qt.io/qt-5/qtwidgets-desktop-systray-example.html

                        also for full blown usage
                        https://github.com/mohabouje/WinToast

                        A 1 Reply Last reply 2 Oct 2022, 10:23
                        0
                        • M mrjj
                          2 Oct 2022, 10:20

                          @Alexey-Serebryakov
                          Ohh.
                          Like Win 10 Toast messages?

                          But you must run on other platforms and prefer your own ?

                          https://doc.qt.io/qt-5/qtwidgets-desktop-systray-example.html

                          also for full blown usage
                          https://github.com/mohabouje/WinToast

                          A Offline
                          A Offline
                          Alexey Serebryakov
                          wrote on 2 Oct 2022, 10:23 last edited by
                          #12

                          @mrjj yes, like Win 10 Toast.
                          Application run on Windows 10 and Ubuntu. So I need show custom popup widget. :-(
                          Thank you but built-in systray not allowed.

                          M 1 Reply Last reply 2 Oct 2022, 10:30
                          0
                          • A Alexey Serebryakov
                            2 Oct 2022, 10:23

                            @mrjj yes, like Win 10 Toast.
                            Application run on Windows 10 and Ubuntu. So I need show custom popup widget. :-(
                            Thank you but built-in systray not allowed.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 2 Oct 2022, 10:30 last edited by
                            #13

                            @Alexey-Serebryakov

                            Ok, it must be totally custom.
                            QSystray also had issues on ubuntu over the years.

                            So is the message also clickable like Toats are or is it more like a tooltip type ?

                            A 1 Reply Last reply 2 Oct 2022, 10:43
                            0
                            • M mrjj
                              2 Oct 2022, 10:30

                              @Alexey-Serebryakov

                              Ok, it must be totally custom.
                              QSystray also had issues on ubuntu over the years.

                              So is the message also clickable like Toats are or is it more like a tooltip type ?

                              A Offline
                              A Offline
                              Alexey Serebryakov
                              wrote on 2 Oct 2022, 10:43 last edited by Alexey Serebryakov 10 Feb 2022, 10:45
                              #14

                              @mrjj Yes there popup widget has title, text, image, close button, links and so on. User can click them and follow to the main window by context dependency. Also popup widget can contain other user-defined custom widget.

                              M 1 Reply Last reply 2 Oct 2022, 11:04
                              0
                              • A Alexey Serebryakov
                                2 Oct 2022, 10:43

                                @mrjj Yes there popup widget has title, text, image, close button, links and so on. User can click them and follow to the main window by context dependency. Also popup widget can contain other user-defined custom widget.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 2 Oct 2022, 11:04 last edited by
                                #15

                                @Alexey-Serebryakov

                                Oh so its a FancyToast widget ;)

                                Well it should not be that hard to place it. and the rich part sounds lovely.

                                Do notice on some virtual Desktops, its like one big screen and you will have to move your window
                                to right x pos as there will be only 1 screen.

                                A 2 Replies Last reply 2 Oct 2022, 11:10
                                0
                                • M mrjj
                                  2 Oct 2022, 11:04

                                  @Alexey-Serebryakov

                                  Oh so its a FancyToast widget ;)

                                  Well it should not be that hard to place it. and the rich part sounds lovely.

                                  Do notice on some virtual Desktops, its like one big screen and you will have to move your window
                                  to right x pos as there will be only 1 screen.

                                  A Offline
                                  A Offline
                                  Alexey Serebryakov
                                  wrote on 2 Oct 2022, 11:10 last edited by Alexey Serebryakov 10 Feb 2022, 11:33
                                  #16

                                  @mrjj what the FancyToast?

                                  1 Reply Last reply
                                  0
                                  • M mrjj
                                    2 Oct 2022, 11:04

                                    @Alexey-Serebryakov

                                    Oh so its a FancyToast widget ;)

                                    Well it should not be that hard to place it. and the rich part sounds lovely.

                                    Do notice on some virtual Desktops, its like one big screen and you will have to move your window
                                    to right x pos as there will be only 1 screen.

                                    A Offline
                                    A Offline
                                    Alexey Serebryakov
                                    wrote on 2 Oct 2022, 13:32 last edited by
                                    #17

                                    @mrjj yeah virtual desktop is next my head ache... :-)

                                    1 Reply Last reply
                                    0
                                    • Axel SpoerlA Offline
                                      Axel SpoerlA Offline
                                      Axel Spoerl
                                      Moderators
                                      wrote on 3 Oct 2022, 07:02 last edited by
                                      #18

                                      Instantiating a QWidget and calling show()create a series of events that get processed asynchronously. It cannot be safely assumed that windowHandle()returns a valid pointer right after construction and/or calling show(). You might be more lucky on Win10, than on UBuntu/X11.

                                      If you want to wait for the widget to be fully constructed, you have to wait for windowHandle() != nullptr, e.g. by

                                      while (!w1.windowHandle())
                                          a.processEvents();
                                      

                                      QWindow::isExposed()tells if the window has been rendered on a screen.

                                      Software Engineer
                                      The Qt Company, Oslo

                                      A 1 Reply Last reply 3 Oct 2022, 09:17
                                      2
                                      • Axel SpoerlA Axel Spoerl
                                        3 Oct 2022, 07:02

                                        Instantiating a QWidget and calling show()create a series of events that get processed asynchronously. It cannot be safely assumed that windowHandle()returns a valid pointer right after construction and/or calling show(). You might be more lucky on Win10, than on UBuntu/X11.

                                        If you want to wait for the widget to be fully constructed, you have to wait for windowHandle() != nullptr, e.g. by

                                        while (!w1.windowHandle())
                                            a.processEvents();
                                        

                                        QWindow::isExposed()tells if the window has been rendered on a screen.

                                        A Offline
                                        A Offline
                                        Alexey Serebryakov
                                        wrote on 3 Oct 2022, 09:17 last edited by
                                        #19

                                        @Axel-Spoerl thank you,
                                        consider example code above,
                                        how can I show w1 widget on first monitor and w2 on second?
                                        Now both widgets showing on the same primary monitor.

                                        Axel SpoerlA 1 Reply Last reply 3 Oct 2022, 11:08
                                        0
                                        • A Alexey Serebryakov
                                          3 Oct 2022, 09:17

                                          @Axel-Spoerl thank you,
                                          consider example code above,
                                          how can I show w1 widget on first monitor and w2 on second?
                                          Now both widgets showing on the same primary monitor.

                                          Axel SpoerlA Offline
                                          Axel SpoerlA Offline
                                          Axel Spoerl
                                          Moderators
                                          wrote on 3 Oct 2022, 11:08 last edited by Axel Spoerl 10 Mar 2022, 11:10
                                          #20

                                          how can I show w1 widget on first monitor and w2 on second?

                                          Suggest you take a look at the documentation and examine QWidget::setScreen(QScreen *screen).

                                          Software Engineer
                                          The Qt Company, Oslo

                                          1 Reply Last reply
                                          0

                                          1/21

                                          1 Oct 2022, 06:16

                                          • Login

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