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. Closing main window and open another one.
QtWS25 Last Chance

Closing main window and open another one.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.1k 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.
  • P Offline
    P Offline
    Pippin
    wrote on last edited by Pippin
    #1

    Hi,

    I'm trying to create a menu window with several QPushButtons so that, depending on which QPushButton the user clicks, the menu window closes and another one opens.

    class Window_A : public QObject
    {
    	Q_OBJECT
    	
    	private:
    	
    		QMainWindow MyWindow;
    		QWidget MyWidget;
    
    		// Some other stuff, including a QPushButton to call the public slot "showWindow_B"
    	
    	public:
    	
    		Window_A(void);
    		~Window_A(void);
    	
    	public slots:
    	
    		void show(void);
    		void showWindow_B(void);
    };
    

    And then the functions are defined as follows:

    Window_A::Window_A(void) : QObject()
    {
    	MyWindow.setWindowTitle("Title");
    	MyWindow.setFixedSize(300, 200);
    	MyWindow.setCentralWidget(&MyWidget);
    }
    
    Window_A::~Window_A(void)
    {
    }
    
    void Window_A::show(void)
    {
    	MyWindow.show();
    }
    
    void Window_A::showWindow_B(void)
    {
    	std::shared_ptr<Window_B> foo = std::make_shared<Window_B>();
    	foo->show();
    	MyWindow.hide();
    }
    

    (Same code for the class Window_B)

    int main(int argc, char *argv[])
    {
    	QApplication app(argc, argv);
    	Window_A foo;
    	foo.show();
    	return app.exec();
    }
    

    However, when I run it and click the appropriate QPushButton so that Window_A::showWindow_B is called, the first window (Window_A) disappears (good) but the new window (Window_B) does not appear (not good). Could anyone point out to me what exactly I'm doing wrong?

    Thanks in advance :)

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

      Hi,

      foo is destroyed at the end of showWindow_B so your Window_B gets destroyed at the same time thus you don't see it.

      Hope it helps

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

      P 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        foo is destroyed at the end of showWindow_B so your Window_B gets destroyed at the same time thus you don't see it.

        Hope it helps

        P Offline
        P Offline
        Pippin
        wrote on last edited by
        #3

        @SGaist

        Thanks for your reply. I'm not sure I understand though, how could foo be destroyed, it's a shared_ptr ?

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

          Even if it's a shared_ptr, it's a class that obeys the same rules as any other. You create your shared pointer locally in your function. It will get destroyed at the end of the function. And since it's the only shared_ptr containing your widget, the widget will get destroyed alongside the shared_ptr object.

          More information here

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

          P 1 Reply Last reply
          1
          • SGaistS SGaist

            Even if it's a shared_ptr, it's a class that obeys the same rules as any other. You create your shared pointer locally in your function. It will get destroyed at the end of the function. And since it's the only shared_ptr containing your widget, the widget will get destroyed alongside the shared_ptr object.

            More information here

            P Offline
            P Offline
            Pippin
            wrote on last edited by
            #5

            @SGaist Thanks a bunch!

            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