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.8k 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 Alexey Serebryakov

    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.

    JonBJ Online
    JonBJ Online
    JonB
    wrote on 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
    • mrjjM mrjj

      @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 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();
      }
      
      mrjjM Z 2 Replies Last reply
      1
      • A Alexey Serebryakov

        @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();
        }
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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
        0
        • mrjjM mrjj

          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 last edited by Alexey Serebryakov
          #8

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

          mrjjM 1 Reply Last reply
          0
          • A Alexey Serebryakov

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            0
            • mrjjM mrjj

              @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 last edited by Alexey Serebryakov
              #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);
              }
              
              mrjjM 1 Reply Last reply
              0
              • A Alexey Serebryakov

                @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);
                }
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #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
                0
                • mrjjM mrjj

                  @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 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.

                  mrjjM 1 Reply Last reply
                  0
                  • A Alexey Serebryakov

                    @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.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • mrjjM mrjj

                      @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 last edited by Alexey Serebryakov
                      #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.

                      mrjjM 1 Reply Last reply
                      0
                      • A Alexey Serebryakov

                        @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.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • mrjjM mrjj

                          @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 last edited by Alexey Serebryakov
                          #16

                          @mrjj what the FancyToast?

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @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 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 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
                              2
                              • Axel SpoerlA Axel Spoerl

                                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 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
                                0
                                • A Alexey Serebryakov

                                  @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 last edited by Axel Spoerl
                                  #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
                                  • A Alexey Serebryakov

                                    @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();
                                    }
                                    
                                    Z Offline
                                    Z Offline
                                    zoujiu
                                    wrote on last edited by
                                    #21

                                    @Alexey-Serebryakov thank you

                                    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