Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open a QMainWindow from QMainWindow and wait until it closes
Forum Updated to NodeBB v4.3 + New Features

Open a QMainWindow from QMainWindow and wait until it closes

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Abdullah RadwanA Offline
    Abdullah RadwanA Offline
    Abdullah Radwan
    wrote on last edited by
    #1

    Hello,

    I am getting a segmentation fault with this code, at the last line:

       SettingsWindow *settingsWin =  new SettingsWindow(this);
    
        settingsWin->show();
    
        QEventLoop loop;
    
        connect(settingsWin, SIGNAL(destroyed()), &loop, SLOT(quit()));
    
        loop.exec();
    
        orbiterPath = settingsWin->orbiterPath;
    

    I want to open the settings window and wait until it closes (Something like .exec() in QDialog). I noticed when debugging all settingsWin variables are not accessible. What is the correct way to open a QMainWindow and wait until it closes?

    K 1 Reply Last reply
    0
    • Abdullah RadwanA Abdullah Radwan

      Hello,

      I am getting a segmentation fault with this code, at the last line:

         SettingsWindow *settingsWin =  new SettingsWindow(this);
      
          settingsWin->show();
      
          QEventLoop loop;
      
          connect(settingsWin, SIGNAL(destroyed()), &loop, SLOT(quit()));
      
          loop.exec();
      
          orbiterPath = settingsWin->orbiterPath;
      

      I want to open the settings window and wait until it closes (Something like .exec() in QDialog). I noticed when debugging all settingsWin variables are not accessible. What is the correct way to open a QMainWindow and wait until it closes?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Abdullah-Radwan

      Hi and welcome to devnet forum

      Not sure if I understand completely what you trying to do. However, when I see the sequence you are giving above and assume that this is a complete block copied from your source, it is the event loop.

         SettingsWindow *settingsWin =  new SettingsWindow(this);    // creates the setting what might be ok
      
          settingsWin->show();                                                                           // shows the window and is ok
      
          QEventLoop loop;                                                                                  // creates event loop
      
          connect(settingsWin, SIGNAL(destroyed()), &loop, SLOT(quit()));                  // connects destroyed signal of settings window to quit loop
      
          loop.exec();                             // starts the loop and until exit respectively quit
      
          orbiterPath = settingsWin->orbiterPath;                       // settingsWindow must be gone at this point
      

      See comments above. The event loop has simply to wait until it is exited. The reason for exit is quit call, but quit is only called when the object is destroyed. When the object is destroyed you cannot access anymore and receive the seg fault.

      Vote the answer(s) that helped you to solve your issue(s)

      Abdullah RadwanA 1 Reply Last reply
      2
      • K koahnig

        @Abdullah-Radwan

        Hi and welcome to devnet forum

        Not sure if I understand completely what you trying to do. However, when I see the sequence you are giving above and assume that this is a complete block copied from your source, it is the event loop.

           SettingsWindow *settingsWin =  new SettingsWindow(this);    // creates the setting what might be ok
        
            settingsWin->show();                                                                           // shows the window and is ok
        
            QEventLoop loop;                                                                                  // creates event loop
        
            connect(settingsWin, SIGNAL(destroyed()), &loop, SLOT(quit()));                  // connects destroyed signal of settings window to quit loop
        
            loop.exec();                             // starts the loop and until exit respectively quit
        
            orbiterPath = settingsWin->orbiterPath;                       // settingsWindow must be gone at this point
        

        See comments above. The event loop has simply to wait until it is exited. The reason for exit is quit call, but quit is only called when the object is destroyed. When the object is destroyed you cannot access anymore and receive the seg fault.

        Abdullah RadwanA Offline
        Abdullah RadwanA Offline
        Abdullah Radwan
        wrote on last edited by Abdullah Radwan
        #3

        @koahnig Thanks for the detailed explanation. I thought .destroy() removes the graphical elements, not the class itself. I removed delete on close attribute and made a custom signal for close event by overriding closeEvent() method. This how it looks in the code:

        Show function in the code:

        SettingsWindow *settingsWin = new SettingsWindow(this);
        
        settingsWin->show();
        
        QEventLoop loop;
        
        connect(settingsWin, SIGNAL(closed()), &loop, SLOT(quit()));
        
        loop.exec();
        

        SettingsWindow.h:

        class SettingsWindow : public QMainWindow
        {
            Q_OBJECT
        
        signals:
            void closed();
        private:
            void closeEvent(QCloseEvent *bar);
        

        SettingsWindow.cpp:

        void SettingsWindow::closeEvent(QCloseEvent *bar){
        
            emit closed();
        
            bar->accept();
        }
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved