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. Window not showing up using show()
QtWS25 Last Chance

Window not showing up using show()

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 18.2k Views
  • 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.
  • nestoracN Offline
    nestoracN Offline
    nestorac
    wrote on last edited by nestorac
    #1

    I have some code for opening a window from the main one, but it doesn't work. "Window open." gets printed, then nothing else happens, it's just as if show() didn't do anything. This is invoked from a menu using a connector and an action.

    The relevant code:

    void test1::openNewWindow()
    {
    qWarning( "Window open." );
    
    QWidget w(this);
    Ui::dialog ventanados;
    ventanados.setupUi(&w);
    w.setWindowModality(Qt::ApplicationModal);
    w.show();
    }
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! The problem is that you create the window on the stack inside openNewWindow(), so it gets destroyed immediately when the function returns.

      void test1::openNewWindow()
      {
      qWarning( "Window open." );
      
      QWidget w(this); // <-- w is local to this scope
      Ui::dialog ventanados;
      ventanados.setupUi(&w);
      w.setWindowModality(Qt::ApplicationModal);
      w.show(); 
      } // <-- scope ends, w gets destroyed
      
      nestoracN 1 Reply Last reply
      4
      • ? A Former User

        Hi! The problem is that you create the window on the stack inside openNewWindow(), so it gets destroyed immediately when the function returns.

        void test1::openNewWindow()
        {
        qWarning( "Window open." );
        
        QWidget w(this); // <-- w is local to this scope
        Ui::dialog ventanados;
        ventanados.setupUi(&w);
        w.setWindowModality(Qt::ApplicationModal);
        w.show(); 
        } // <-- scope ends, w gets destroyed
        
        nestoracN Offline
        nestoracN Offline
        nestorac
        wrote on last edited by
        #3

        @Wieland thank you. I have tried it and it is an excellent answer, but I don't still get the clue. I have added a 1sec delay to the function after calling show() to see if the windows appears, no luck. I have made the changes you have proposed, and I now have the following code:

        test1.h

        #ifndef test1_H
        #define test1_H
        
        #include <QMainWindow>
        #include <QWidget>
        
        #include "ui_test1.h"
        
        class test1 : public QMainWindow
        {
            Q_OBJECT
        
        public:
            test1();
            
        public slots:
           void openNewWindow();
           
        private:
            QWidget *w;
            Ui::dialog ventanados;
        };
        
        #endif // test1_H
        

        test1.cpp

        #include "test1.h"
        
        #include <QLabel>
        #include <QMenu>
        #include <QMenuBar>
        #include <QAction>
        #include <QDialog>
        #include <QtTest/QTest>
        
        test1::test1()
        {
            w = new QWidget(this);
            QLabel* label = new QLabel( this );
            label->setText( "Hello World!" );
            setCentralWidget( label );
            QAction* action = new QAction(this);
            action->setText( "Open new window" );
            connect(action, SIGNAL(triggered()), SLOT(openNewWindow()) );
            menuBar()->addMenu( "File" )->addAction( action );
        }
        
        void test1::openNewWindow()
        {
            ventanados.setupUi(w);
            w->show();
            QTest::qSleep(1000);
        }
        
        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          The following creates a widget that is a child of your MainWindow:

          w = new QWidget(this);

          If you want w to be a window on its own, then call the constructor without a parent:
          w = new QWidget();

          1 Reply Last reply
          4
          • nestoracN Offline
            nestoracN Offline
            nestorac
            wrote on last edited by
            #5

            OMG, thanks!!!

            1 Reply Last reply
            1
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Parents are important for stacking order and focus management. What's more is in your code you're not deleting it explicitly, so if you don't give it a parent it will leak.
              Better than not giving it a parent would be to either use QDialog instead or make it a window via a flag, i.e.:

              w = new QWidget(this, Qt::Window);
              
              1 Reply Last reply
              4
              • nestoracN Offline
                nestoracN Offline
                nestorac
                wrote on last edited by
                #7

                It also works! Thank you.

                1 Reply Last reply
                1

                • Login

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