0

In Qt I have a 2 forms say FirstUI and SecondUI. The main opens the FirstUI. Here I check if the databases needed for the application are present and if not present creates a new one. It also checks if there are any wifi network details stored in the database. If there are details of the last connected wifi, then the application scans for available networks and connects to the wifi network using the details from the database.

Now if there is no wifi detail in the database or if the network listed in the database is not present or if the application was unable to connect to the wifi network it will emit a signal WifiNotConnected();

I have connected the signal to a slot that opens the SecondUI.

connect(this,SIGNAL(WifiNotConnected()),this,SLOT(OpenSecondUI()));
.....

void FirstUI::OpenSecondUI()
{
   SecondUI *sec = new SecondUI();
   this->close();
   sec->show();
}

The SecondUI opens, but this does not close the FirstUI.

EDIT: If the wifi details are present, I have a class (WifiBoot) that inherits QObject and does the connection tasks for me. Since I want the GIF file to be played in the GUI and the connection to occur same time I have instantiated the class (WifiBoot) that does the wifi connection and moved it to another thread. After the wifi is connected I emit the finished signal which is connected to the slot to open the SecondUI

connect(worker,SIGNAL(finished()),this,SLOT(FinishedConnection()));

void FirstUI::FinishedConnection()
{
   OpenSecondUI();
}

Here it closes the FirstUI and opens the SecondUI. But in the first case it does not. Why is this happening? Is there a better way to go about it? Any help is appreciated

gfernandes
  • 1,156
  • 3
  • 12
  • 29
  • See if this works: Make sure the modality of second ui is `NonModal`. Then in your code reverse the order, first show the second ui, then close the current one. (`sec->show(); this->close();`) – Mousa Feb 26 '14 at 11:10
  • 1
    Probably OT but I cannot resist. I would suggest to make a controller to accomplish your goal. The controller will manage both UI so you can decouple UIs. I don't think is a good practice that a UI opens another UI.. – andrea.marangoni Feb 26 '14 at 11:51
  • @andrea.marangoni Thank you for your reply. Could you please provide an example code? – gfernandes Feb 26 '14 at 12:24
  • @andrea.marangoni It is an application having about 30+ UI. So having a controller class is the right way to go about? – gfernandes Feb 26 '14 at 12:32
  • andrea.marangoni gave the best approach. You should have 1 main window and call dialog boxes. The FirstUI might not be closing, because it calls the SecondUI. I might be wrong, but I think the SecondUI's visibility is dependant on the FirstUI. If the FirstUI dies the SecondUI has lost all references and will be destroyed. You could just hide the FirstUI or you just have the Signal attached to a QMainWindow where it closes the FirstUI and opens the SecondUI. – justengel Feb 26 '14 at 12:55
  • FirstUI is not the parent of the SecondUI. Also, it opens SecondUI and closes FirstUI if the signal is emitted in the class WifiBoot stating the wifi is connected. If I emit the signal in the FirstUI class and connect it to the slot openSecondUI(), it executes the slot openSecondUI() and even returns true for this->close() also opens SecondUI but does not close FirstUI. – gfernandes Feb 26 '14 at 13:35
  • @Giz I don t have example code and I don't have time to make it. What I mean is that you should have a class that manage to open, close, hide etc.. your UIs. The purpose of a UI is to show data and interact with the user. The purpose of a UI is NOT to open other UIs or make anything else than showing data. How a UI interact with user? with buttons, dialogs, etc.. Who is responsible to do everything but showing data and interact with user? a class usually called 'controller'. search on google for "Model View Controller Design pattern" – andrea.marangoni Feb 26 '14 at 14:58
  • Thank you @andrea.marangoni. Link to the code http://www.qtcentre.org/threads/58231-On-calling-close()-the-ui-does-not-close Is this a wrong approach? – gfernandes Feb 26 '14 at 15:17
  • 1
    @Giz I would say that you are close to the approach I would take. the 'worker' could be a member. Don't do everything inside the constructor of `MainWindow`. you don't need to emit events inside the same class, you just need a conditional. this are basic programming principle so I suggest to read a bit more on design principle.. – andrea.marangoni Feb 26 '14 at 15:42

2 Answers2

0

first check if

this->close();

returns true. the other thing might be to just hide it using

QWidget::hide()

as well as set the FirstUI as parent of the SecondUI so your application will not leak memory IF you have multiple instances of FirstUI. (Forget the parent thing if you still close() the widget)

cheers

Zaiborg
  • 2,492
  • 19
  • 28
0

The OpenSecondUI() was called in the constructor itself. Therefore the close() in OpenSecondUI() was taking place before the UI was up and running. To solve this as suggested in the QT Forum and by @andrea.marangoni hint of the constructor being too populated, I used a single shot timer and moved the entire code in the constructor to a slot and called the slot after a certain delay. This ensured that before the slot OpenSecondUI() was called, the UI was up and running.

gfernandes
  • 1,156
  • 3
  • 12
  • 29