SetVisible(true)/SetVisible(false) or hide ()/show() error
-
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:
#include <QMainWindow> ... private: Ui::MainWindow *ui;
#include <QMainWindow> #include <mainwindow.h> ... private: Ui::promediation *prom; MainWindow *main;
2. Forms:
#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(); }
#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!
-
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.
-
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 thedeleteOnClose
attribute or handle the creation to only use one and re-use it. -
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. -
@Pl45m4 Thank you guys.
After your suggestions I solved the problem.
- Headers:
#include <QMainWindow>
...
private: Ui::MainWindow *ui;
...
public slots: void openWindow();
#include <QMainWindow>
...
signals: void exec_closeW();
private: Ui::promediation *prom; MainWindow *main;
- Forms:
#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(); }
#include "mainwindow.h"
void promediation::on_pushButton_clicked() { emit exec_closeW(); //now everything works! }
Thank you all!!