Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top