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. SetVisible(true)/SetVisible(false) or hide ()/show() error
Forum Updated to NodeBB v4.3 + New Features

SetVisible(true)/SetVisible(false) or hide ()/show() error

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

    Hi,
    I have two forms. Each one has a push_button to change the window to another one. The problem is: I can hide or SetVisible(false) anyone but i can't show or SetVisible (true) the other.

    1. Headers:

    //MainWindow.h

    #include <QMainWindow>
    ...
    private:
        Ui::MainWindow *ui;
    

    //promediation.h

    #include <QMainWindow>
    #include <mainwindow.h>
    ...
    
    private:
    Ui::promediation *prom;
    MainWindow *main;
    

    2. Forms:

    //MainWindow.cpp

    #include "promediation.h"

    void MainWindow::on_pushButton_clicked() // hide "MainWindow" and show de "promediation"
    {
    
        setVisible(false); // or hide() - it works!
        promediation *prom;
        prom = new promediation (this);
        prom->show();
    }
    

    //promediation.cpp

    #include "mainwindow.h"
    
    void promediation::on_pushButton_clicked()
    {
        setVisible(false); // or hide() - it works!
      
       main->setVisible(true); // or  main->show() - it doesn't work!
    
    }
    

    The error is:

    23:17:26: The program has unexpectedly finished.
    23:17:26: The process was ended forcefully.
    23:17:26: /Users/../SmartSimEP crashed.

    If I create the form again it works:

    main = new MainWindow(this);
    main->show();
    

    but I don't want to create, I want to show again.

    Thanks!

    Pl45m4P 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      main->setVisible(true);

      What is main here ? Where are you creating the object of this ? Without assigning the object, it is bound to crash.

      If your idea is A->B & B->A, ensure the both objects are known to each other somehow.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

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

        Hi,

        Looks like a good case for signal and slots interaction. You should add a signal to your promediation widget that you will trigger when it's closed and connect that to the show slot of your MainWindow object.

        Note that from the looks of it, you are going to create new promediation widgets all the time without cleaning them. So either set the deleteOnClose attribute or handle the creation to only use one and re-use it.

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

        1 Reply Last reply
        5
        • P pedromenezes

          Hi,
          I have two forms. Each one has a push_button to change the window to another one. The problem is: I can hide or SetVisible(false) anyone but i can't show or SetVisible (true) the other.

          1. Headers:

          //MainWindow.h

          #include <QMainWindow>
          ...
          private:
              Ui::MainWindow *ui;
          

          //promediation.h

          #include <QMainWindow>
          #include <mainwindow.h>
          ...
          
          private:
          Ui::promediation *prom;
          MainWindow *main;
          

          2. Forms:

          //MainWindow.cpp

          #include "promediation.h"

          void MainWindow::on_pushButton_clicked() // hide "MainWindow" and show de "promediation"
          {
          
              setVisible(false); // or hide() - it works!
              promediation *prom;
              prom = new promediation (this);
              prom->show();
          }
          

          //promediation.cpp

          #include "mainwindow.h"
          
          void promediation::on_pushButton_clicked()
          {
              setVisible(false); // or hide() - it works!
            
             main->setVisible(true); // or  main->show() - it doesn't work!
          
          }
          

          The error is:

          23:17:26: The program has unexpectedly finished.
          23:17:26: The process was ended forcefully.
          23:17:26: /Users/../SmartSimEP crashed.

          If I create the form again it works:

          main = new MainWindow(this);
          main->show();
          

          but I don't want to create, I want to show again.

          Thanks!

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @pedromenezes

          Hi,

          I think I have a solution for this ( @SGaist Just using Signals & Slots would have the same effect).
          Had this problem in one of my projects before.

          Somewhere, literally somewhere (I just tried to quote it, but I couldn't find it again. Somewhere around hide(), Window-Visibility and show/ close events), in the Qt Doc is a little hint that says, that the main program gets terminated by default, if you hide the last remaining window even if there is some action running in the background.
          (and this is what your code does: hides mainwin, opens premediation-window, hides premediation-window, tries to show mainwindow again, crash)

          What I did and what you could do, is overriding your premediation-window CloseEvent and emit a signal or call a function, which will show your MainWindow, then continue with the closeEvent.

          EDIT:
          Your header structure seems confusing as well. You renamed your prom-UI to prom, same name as premed instance? (Is just here in forum or in your actual code?) and in promheader there is a pointer to your mainwindow?
          Like @dheerendra said, if you want these two windows to show/hide each other, they have to know about each other first.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          P 1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @pedromenezes

            Hi,

            I think I have a solution for this ( @SGaist Just using Signals & Slots would have the same effect).
            Had this problem in one of my projects before.

            Somewhere, literally somewhere (I just tried to quote it, but I couldn't find it again. Somewhere around hide(), Window-Visibility and show/ close events), in the Qt Doc is a little hint that says, that the main program gets terminated by default, if you hide the last remaining window even if there is some action running in the background.
            (and this is what your code does: hides mainwin, opens premediation-window, hides premediation-window, tries to show mainwindow again, crash)

            What I did and what you could do, is overriding your premediation-window CloseEvent and emit a signal or call a function, which will show your MainWindow, then continue with the closeEvent.

            EDIT:
            Your header structure seems confusing as well. You renamed your prom-UI to prom, same name as premed instance? (Is just here in forum or in your actual code?) and in promheader there is a pointer to your mainwindow?
            Like @dheerendra said, if you want these two windows to show/hide each other, they have to know about each other first.

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

            @Pl45m4 Thank you guys.

            After your suggestions I solved the problem.

            1. Headers:

            //MainWindow.h

            #include <QMainWindow>
            

            ...

            private:
                Ui::MainWindow *ui;
            

            ...

            public slots:
                void openWindow();
            

            //promediation.h

            #include <QMainWindow>
            
            

            ...

            signals:
                void exec_closeW();
            
            
            private:
            Ui::promediation *prom;
            MainWindow *main;
            
            1. Forms:

            //MainWindow.cpp

            #include "promediation.h"
            

            ...

            void MainWindow::openWindow(){
            
            this->show();
            delete prom;
            
            }
            
            void MainWindow::on_pushButton_clicked() // hide "MainWindow" and show de "promediation"
            {
                prom = new promediation();
                prom->show();
                QObject::connect(prom, SIGNAL(exec_closeW()), this, SLOT(openWindow()));
                this->hide();
            }
            

            //promediation.cpp

            #include "mainwindow.h"
            
            void promediation::on_pushButton_clicked()
            {
                emit exec_closeW(); //now everything works!
            }
            

            Thank you all!!

            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