Results 1 to 6 of 6

Thread: On calling close() the ui does not close

  1. #1
    Join Date
    Aug 2013
    Posts
    25
    Thanks
    7
    Qt products
    Qt5

    Default On calling close() the ui does not close

    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.

    Qt Code:
    1. connect(this,SIGNAL(WifiNotConnected()),this,SLOT(OpenSecondUI()));
    2. .....
    3.  
    4. void FirstUI::OpenSecondUI()
    5. {
    6. SecondUI *sec = new SecondUI();
    7. this->close();
    8. sec->show();
    9. }
    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

    Qt Code:
    1. connect(worker,SIGNAL(finished()),this,SLOT(FinishedConnection()));
    2.  
    3. void FirstUI::FinishedConnection()
    4. {
    5. OpenSecondUI();
    6. }
    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

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QMovie>
    6. #include <QThread>
    7. #include "databasemanager.h"
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private slots:
    22. void openSecondUI();
    23.  
    24. void createDatabaseTables();
    25.  
    26. void ConnectWifi();
    27.  
    28. void DatabaseError(QString);
    29.  
    30. void NotConnected();
    31.  
    32. void FinishedConnecting();
    33.  
    34.  
    35. signals:
    36. void NoWifiConnection();
    37.  
    38. void CallSlotToConnectWifi();
    39.  
    40. private:
    41. Ui::MainWindow *ui;
    42.  
    43. int value;
    44.  
    45. DataBaseManager *dbm;
    46.  
    47. QMovie *movie;
    48.  
    49. QThread *Connect_to_wifi_thread;
    50. };
    51.  
    52. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.c
    Qt Code:
    1. #include <QTimer>
    2. #include <QDebug>
    3. #include <QFuture>
    4. #include <QProcess>
    5. #include "secondui.h"
    6. #include "secondwindow.h"
    7. #include "mainwindow.h"
    8. #include "ui_mainwindow.h"
    9. #include "wificonnecting.h"
    10. #include "wifibootconnect.h"
    11.  
    12. #define DATABASE_NAME "UniSettings"
    13. #define NOT_CONNECTED "NA"
    14. #define CONNECTED "Connected"
    15. #define ASSIGN_NULL ""
    16.  
    17. MainWindow::MainWindow(QWidget *parent) :
    18. QMainWindow(parent),
    19. ui(new Ui::MainWindow)
    20. {
    21. ui->setupUi(this);
    22.  
    23. //Play a gif movie on qlabel
    24. movie = new QMovie(":/GIFfiles/GIFfiles/WelcomeCircular.GIF");
    25. this->ui->GIFLabel->setMovie(movie);
    26. movie->start();
    27.  
    28. //Initializing the int variable
    29. value = 0;
    30.  
    31. connect(this,SIGNAL(NoWifiConnection()),this,SLOT(NotConnected()));
    32.  
    33. //Connecting to the database
    34. //DataBaseManager class instantiation
    35. dbm = new DataBaseManager;
    36.  
    37. if(!(dbm->ifopen()))
    38. {
    39. qDebug() << "Connecting";
    40.  
    41. //Open the database and return the status
    42. bool checkOpen = dbm->openDB(DATABASE_NAME);
    43.  
    44. //If open display message connected
    45. if(checkOpen)
    46. {
    47. qDebug() << "connected to database";
    48. createDatabaseTables();
    49. }
    50. //else display message not exist
    51. else
    52. {
    53. qDebug() << "Database does not exist";
    54. }
    55. }
    56.  
    57. //If any known wifi exists connect to it
    58. QSqlQuery *qry = new QSqlQuery(dbm->db);
    59.  
    60. if(qry->exec("SELECT * FROM LastState WHERE name=\'Wifi\'"))
    61. {
    62. //If it matches then get all the data from that rowNotConnected();
    63. if(qry->next())
    64. {
    65. QString Connection_State = qry->value(2).toString();
    66. qDebug() << Connection_State;
    67. QString Connection_essid = qry->value(3).toString();
    68. qDebug() <<Connection_essid;
    69.  
    70. if(Connection_State== NOT_CONNECTED)
    71. {
    72. qDebug() << "Not connected to any wifi before shutdown";
    73.  
    74. emit NoWifiConnection();
    75. }
    76. else
    77. {
    78. //Call function to connect to wifi in another thread
    79. qDebug() << "connected";
    80. ConnectWifi();
    81. }
    82. }
    83. }
    84. else
    85. {
    86. qDebug() << dbm->lastError().text();
    87. }
    88.  
    89.  
    90.  
    91. }
    92.  
    93. MainWindow::~MainWindow()
    94. {
    95. delete ui;
    96.  
    97. qDebug() << "Welcome";
    98.  
    99. qDebug() << "Closing the connection to the database";
    100.  
    101. //Close the database connection
    102. dbm->closeDB();
    103. }
    104.  
    105. /*
    106.  *@brief: Function to open the SecondUI class
    107.  *
    108.  */
    109. void MainWindow::openSecondUI()
    110. {
    111. //Instantiate the SecondUI class
    112. SecondUI *sec = new SecondUI();
    113.  
    114. //Show the SecondUI ui
    115. sec->show();
    116.  
    117. //Close MainWindow class
    118. this->close();
    119. }
    120.  
    121. void MainWindow::createDatabaseTables()
    122. {
    123. bool success = dbm->createBTDeviceTable();
    124. qDebug() << success;
    125.  
    126. success = dbm->createBTPairedDeviceTable();
    127. qDebug() << success;
    128.  
    129. ......
    130.  
    131. success = dbm->create_LastState_Table();
    132. qDebug() << success;
    133. }
    134.  
    135. void MainWindow::DatabaseError(QString err)
    136. {
    137. qDebug() << "Database error EMITTED: " << err;
    138. }
    139.  
    140. void MainWindow::NotConnected()
    141. {
    142. qDebug() << "Not connected EMITTED" ;
    143. openSecondUI();
    144. }
    145.  
    146. void MainWindow::FinishedConnecting()
    147. {
    148. qDebug() << "Finished emitted by boot up wifi process";
    149. openSecondUI();
    150. }
    151.  
    152. void MainWindow::ConnectWifi()
    153. {
    154. qDebug() << "I am here";
    155. Connect_to_wifi_thread = new QThread;
    156.  
    157. WifiBootConnect *worker = new WifiBootConnect();
    158.  
    159. worker->moveToThread(Connect_to_wifi_thread);
    160.  
    161. //SIGNALS AND SLOTS
    162. connect(worker,SIGNAL(DataBaseError(QString)),this,SLOT(DatabaseError(QString)));
    163. connect(worker,SIGNAL(NotConnected()),this,SLOT(NotConnected()));
    164. connect(worker,SIGNAL(finished()),this,SLOT(FinishedConnecting()));
    165. connect(Connect_to_wifi_thread,SIGNAL(started()),worker,SLOT(ConnectWifiProcess()));
    166. connect(worker,SIGNAL(finished()),Connect_to_wifi_thread,SLOT(quit()));
    167. connect(worker,SIGNAL(finished()),worker,SLOT(deleteLater()));
    168. connect(Connect_to_wifi_thread,SIGNAL(finished()),Connect_to_wifi_thread,SLOT(deleteLater()));
    169.  
    170. //Start the thread
    171. Connect_to_wifi_thread->start();
    172. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by gfernandes; 26th February 2014 at 16:18.

  2. #2
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: On calling close() the ui does not close

    How bout disconnecting your signal and slot?












    Don't trust me. My rank's novice
    Last edited by ttimt; 26th February 2014 at 13:15.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: On calling close() the ui does not close

    Quote Originally Posted by gfernandes View Post
    The SecondUI opens, but this does not close the FirstUI. Why is this happening? Is there a better way to go about it?
    Nothing obvious wrong with the presented code.
    Have you tried hide() instead of close()?

    Since FirstUI seems to be your primary UI, it might actually make sense to keep it around to the time SecondUI is finished, no?

    Cheers,
    _

  4. #4
    Join Date
    Aug 2013
    Posts
    25
    Thanks
    7
    Qt products
    Qt5

    Default Re: On calling close() the ui does not close

    Dear Anda_skoa,

    Thank you for your reply. I have tried hide(); it does not work either. I checked the return value of this->close(); and it is true

    Basically I have 30+ ui which have QWidget as the base class designed using QtDesigner.

    I will never return back to the FirstUI, because I start my application there by loading all the possible databases required for the application. Then I moved ahead with the SecondUI which is the user login.

    Once logged in I have a selectionMenu which has 4 pushbuttons.

    The link shows the flow
    http://stackoverflow.com/questions/2...ui-forms-in-qt

    Now the simplest way to go about is using this->close and showing the nextUI

    Is it a bad approach?

    I am a java/android developer and this project is my first with Qt. I am not very sure how to go about it.


    Added after 5 minutes:


    @ttimt
    thank you for your reply. I tried that, does not work either
    Last edited by gfernandes; 26th February 2014 at 16:18.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: On calling close() the ui does not close

    One problem that I can see is the emit in the constructor.

    If you program reaches that emit, it will invoke the slot and show the second UI.
    The close is meaningless at this point because "this" has not be shown yet.
    In fact the close at this point is followed by a show() in main().

    My suggestion is to make sure the window is up and running before attempting any thing else.

    To do that move any constructor code after
    Qt Code:
    1. connect(this,SIGNAL(NoWifiConnection()),this,SLOT(NotConnected()));
    To copy to clipboard, switch view to plain text mode 
    into a new slot and call this slot "delayed", i.e. after the event loop has started processing events
    Qt Code:
    1. QTimer::singleShot(0, this, SLOT(theNewSlot()));
    To copy to clipboard, switch view to plain text mode 

    Actually you can remove the signal and the connect and just call openSecondUI() directly when you encouter you need it.
    I.e. no need to send a signal to "yourself".

    Cheers,
    _

    P.S.: if you run all your UIs "full screen" on the device, then the suggestion on stackoverflow to use a QStackedWidget is indeed the way to go unless each UI is so large on memory consumption that you need to get rid of the previous screen when you show a new one.

  6. The following user says thank you to anda_skoa for this useful post:

    gfernandes (27th February 2014)

  7. #6
    Join Date
    Aug 2013
    Posts
    25
    Thanks
    7
    Qt products
    Qt5

    Default Re: On calling close() the ui does not close

    @anda_skoa

    YAYYY You are awesome!! How I wish I had a guide like you, a recent college graduate like me would actually be a better programmer..

    Thank you so much yayyy

    Giz

Similar Threads

  1. QML: Component.onDestruction not calling on window close
    By nestuser in forum Qt Programming
    Replies: 6
    Last Post: 22nd October 2012, 15:09
  2. midi child does not close when I call close()
    By qlands in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2011, 23:25
  3. Replies: 2
    Last Post: 17th December 2010, 20:01
  4. QTemporaryFile stays locked even after calling close()
    By Tiansen in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2010, 14:35
  5. Calling destructors on close event
    By gyre in forum Newbie
    Replies: 22
    Last Post: 27th November 2007, 20:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.