Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Closing main window and open another one.

    General and Desktop
    2
    5
    1821
    Loading More Posts
    • 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
      Pippin last edited by Pippin

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • P
          Pippin @SGaist last edited by

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • P
              Pippin @SGaist last edited by

              @SGaist Thanks a bunch!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post