In Qt I have a 2 forms say FirstUI and SecondUI. The main.cpp 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();
}
connect(this,SIGNAL(WifiNotConnected()),this,SLOT(OpenSecondUI()));
.....
void FirstUI::OpenSecondUI()
{
SecondUI *sec = new SecondUI();
this->close();
sec->show();
}
To copy to clipboard, switch view to plain text mode
The SecondUI opens, but this does not close the FirstUI.
EDIT 1: 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();
}
connect(worker,SIGNAL(finished()),this,SLOT(FinishedConnection()));
void FirstUI::FinishedConnection()
{
OpenSecondUI();
}
To copy to clipboard, switch view to plain text mode
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
Any help is appreciated
EDIT 2
My code
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMovie>
#include <QThread>
#include "databasemanager.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void openSecondUI();
void createDatabaseTables();
void ConnectWifi();
void NotConnected();
void FinishedConnecting();
signals:
void NoWifiConnection();
void CallSlotToConnectWifi();
private:
Ui::MainWindow *ui;
int value;
DataBaseManager *dbm;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMovie>
#include <QThread>
#include "databasemanager.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void openSecondUI();
void createDatabaseTables();
void ConnectWifi();
void DatabaseError(QString);
void NotConnected();
void FinishedConnecting();
signals:
void NoWifiConnection();
void CallSlotToConnectWifi();
private:
Ui::MainWindow *ui;
int value;
DataBaseManager *dbm;
QMovie *movie;
QThread *Connect_to_wifi_thread;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.c
#include <QTimer>
#include <QDebug>
#include <QFuture>
#include <QProcess>
#include "secondui.h"
#include "secondwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wificonnecting.h"
#include "wifibootconnect.h"
#define DATABASE_NAME "UniSettings"
#define NOT_CONNECTED "NA"
#define CONNECTED "Connected"
#define ASSIGN_NULL ""
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Play a gif movie on qlabel
movie
= new QMovie(":/GIFfiles/GIFfiles/WelcomeCircular.GIF");
this->ui->GIFLabel->setMovie(movie);
movie->start();
//Initializing the int variable
value = 0;
connect(this,SIGNAL(NoWifiConnection()),this,SLOT(NotConnected()));
//Connecting to the database
//DataBaseManager class instantiation
dbm = new DataBaseManager;
if(!(dbm->ifopen()))
{
qDebug() << "Connecting";
//Open the database and return the status
bool checkOpen = dbm->openDB(DATABASE_NAME);
//If open display message connected
if(checkOpen)
{
qDebug() << "connected to database";
createDatabaseTables();
}
//else display message not exist
else
{
qDebug() << "Database does not exist";
}
}
//If any known wifi exists connect to it
if(qry->exec("SELECT * FROM LastState WHERE name=\'Wifi\'"))
{
//If it matches then get all the data from that rowNotConnected();
if(qry->next())
{
QString Connection_State
= qry
->value
(2).
toString();
qDebug() << Connection_State;
QString Connection_essid
= qry
->value
(3).
toString();
qDebug() <<Connection_essid;
if(Connection_State== NOT_CONNECTED)
{
qDebug() << "Not connected to any wifi before shutdown";
emit NoWifiConnection();
}
else
{
//Call function to connect to wifi in another thread
qDebug() << "connected";
ConnectWifi();
}
}
}
else
{
qDebug() << dbm->lastError().text();
}
}
MainWindow::~MainWindow()
{
delete ui;
qDebug() << "Welcome";
qDebug() << "Closing the connection to the database";
//Close the database connection
dbm->closeDB();
}
/*
*@brief: Function to open the SecondUI class
*
*/
void MainWindow::openSecondUI()
{
//Instantiate the SecondUI class
SecondUI *sec = new SecondUI();
//Show the SecondUI ui
sec->show();
//Close MainWindow class
this->close();
}
void MainWindow::createDatabaseTables()
{
bool success = dbm->createBTDeviceTable();
qDebug() << success;
success = dbm->createBTPairedDeviceTable();
qDebug() << success;
......
success = dbm->create_LastState_Table();
qDebug() << success;
}
void MainWindow
::DatabaseError(QString err
) {
qDebug() << "Database error EMITTED: " << err;
}
void MainWindow::NotConnected()
{
qDebug() << "Not connected EMITTED" ;
openSecondUI();
}
void MainWindow::FinishedConnecting()
{
qDebug() << "Finished emitted by boot up wifi process";
openSecondUI();
}
void MainWindow::ConnectWifi()
{
qDebug() << "I am here";
Connect_to_wifi_thread
= new QThread;
WifiBootConnect *worker = new WifiBootConnect();
worker->moveToThread(Connect_to_wifi_thread);
//SIGNALS AND SLOTS
connect(worker,
SIGNAL(DataBaseError
(QString)),
this,
SLOT(DatabaseError
(QString)));
connect(worker,SIGNAL(NotConnected()),this,SLOT(NotConnected()));
connect(worker,SIGNAL(finished()),this,SLOT(FinishedConnecting()));
connect(Connect_to_wifi_thread,SIGNAL(started()),worker,SLOT(ConnectWifiProcess()));
connect(worker,SIGNAL(finished()),Connect_to_wifi_thread,SLOT(quit()));
connect(worker,SIGNAL(finished()),worker,SLOT(deleteLater()));
connect(Connect_to_wifi_thread,SIGNAL(finished()),Connect_to_wifi_thread,SLOT(deleteLater()));
//Start the thread
Connect_to_wifi_thread->start();
}
#include <QTimer>
#include <QDebug>
#include <QFuture>
#include <QProcess>
#include "secondui.h"
#include "secondwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wificonnecting.h"
#include "wifibootconnect.h"
#define DATABASE_NAME "UniSettings"
#define NOT_CONNECTED "NA"
#define CONNECTED "Connected"
#define ASSIGN_NULL ""
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Play a gif movie on qlabel
movie = new QMovie(":/GIFfiles/GIFfiles/WelcomeCircular.GIF");
this->ui->GIFLabel->setMovie(movie);
movie->start();
//Initializing the int variable
value = 0;
connect(this,SIGNAL(NoWifiConnection()),this,SLOT(NotConnected()));
//Connecting to the database
//DataBaseManager class instantiation
dbm = new DataBaseManager;
if(!(dbm->ifopen()))
{
qDebug() << "Connecting";
//Open the database and return the status
bool checkOpen = dbm->openDB(DATABASE_NAME);
//If open display message connected
if(checkOpen)
{
qDebug() << "connected to database";
createDatabaseTables();
}
//else display message not exist
else
{
qDebug() << "Database does not exist";
}
}
//If any known wifi exists connect to it
QSqlQuery *qry = new QSqlQuery(dbm->db);
if(qry->exec("SELECT * FROM LastState WHERE name=\'Wifi\'"))
{
//If it matches then get all the data from that rowNotConnected();
if(qry->next())
{
QString Connection_State = qry->value(2).toString();
qDebug() << Connection_State;
QString Connection_essid = qry->value(3).toString();
qDebug() <<Connection_essid;
if(Connection_State== NOT_CONNECTED)
{
qDebug() << "Not connected to any wifi before shutdown";
emit NoWifiConnection();
}
else
{
//Call function to connect to wifi in another thread
qDebug() << "connected";
ConnectWifi();
}
}
}
else
{
qDebug() << dbm->lastError().text();
}
}
MainWindow::~MainWindow()
{
delete ui;
qDebug() << "Welcome";
qDebug() << "Closing the connection to the database";
//Close the database connection
dbm->closeDB();
}
/*
*@brief: Function to open the SecondUI class
*
*/
void MainWindow::openSecondUI()
{
//Instantiate the SecondUI class
SecondUI *sec = new SecondUI();
//Show the SecondUI ui
sec->show();
//Close MainWindow class
this->close();
}
void MainWindow::createDatabaseTables()
{
bool success = dbm->createBTDeviceTable();
qDebug() << success;
success = dbm->createBTPairedDeviceTable();
qDebug() << success;
......
success = dbm->create_LastState_Table();
qDebug() << success;
}
void MainWindow::DatabaseError(QString err)
{
qDebug() << "Database error EMITTED: " << err;
}
void MainWindow::NotConnected()
{
qDebug() << "Not connected EMITTED" ;
openSecondUI();
}
void MainWindow::FinishedConnecting()
{
qDebug() << "Finished emitted by boot up wifi process";
openSecondUI();
}
void MainWindow::ConnectWifi()
{
qDebug() << "I am here";
Connect_to_wifi_thread = new QThread;
WifiBootConnect *worker = new WifiBootConnect();
worker->moveToThread(Connect_to_wifi_thread);
//SIGNALS AND SLOTS
connect(worker,SIGNAL(DataBaseError(QString)),this,SLOT(DatabaseError(QString)));
connect(worker,SIGNAL(NotConnected()),this,SLOT(NotConnected()));
connect(worker,SIGNAL(finished()),this,SLOT(FinishedConnecting()));
connect(Connect_to_wifi_thread,SIGNAL(started()),worker,SLOT(ConnectWifiProcess()));
connect(worker,SIGNAL(finished()),Connect_to_wifi_thread,SLOT(quit()));
connect(worker,SIGNAL(finished()),worker,SLOT(deleteLater()));
connect(Connect_to_wifi_thread,SIGNAL(finished()),Connect_to_wifi_thread,SLOT(deleteLater()));
//Start the thread
Connect_to_wifi_thread->start();
}
To copy to clipboard, switch view to plain text mode
Bookmarks