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. Qt MainWindow how to close and reopen
Forum Updated to NodeBB v4.3 + New Features

Qt MainWindow how to close and reopen

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 1.9k Views 1 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I've added an import function to my application that imports an SQL file into MariaDB, this works ok, I want to cause the dialog that the import button is present on to reopen causing the table view to repopulate with the database records.

    Is this possible with existing signals?

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I've added an import function to my application that imports an SQL file into MariaDB, this works ok, I want to cause the dialog that the import button is present on to reopen causing the table view to repopulate with the database records.

      Is this possible with existing signals?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @SPlatten Not sure I understand your description. But it is of course possible to create main window instance and show it. Not sure what the problem is.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      SPlattenS 1 Reply Last reply
      0
      • jsulmJ jsulm

        @SPlatten Not sure I understand your description. But it is of course possible to create main window instance and show it. Not sure what the problem is.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @jsulm , what I want to do is trigger all the initial checks that are performed when the window is first opened. Presently, when I click the import button it imports the SQL file into the database and this works fine, the dialog doesn't display the data, if I close the dialog then open it again, the data is displayed.

        What I want to do is trigger these actions automatically after the import completes so the dialog shows the data automatically.

        Kind Regards,
        Sy

        jsulmJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @jsulm , what I want to do is trigger all the initial checks that are performed when the window is first opened. Presently, when I click the import button it imports the SQL file into the database and this works fine, the dialog doesn't display the data, if I close the dialog then open it again, the data is displayed.

          What I want to do is trigger these actions automatically after the import completes so the dialog shows the data automatically.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @SPlatten said in Qt MainWindow how to close and reopen:

          What I want to do is trigger these actions automatically after the import completes so the dialog shows the data automatically.

          Well, I don't know your code, so can't say why it does not show the data in the dialog. You will need to show your code.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          SPlattenS 1 Reply Last reply
          0
          • AxelViennaA Offline
            AxelViennaA Offline
            AxelVienna
            wrote on last edited by
            #5

            Closing and re-launching your entire main window in your case seems too much of the good thing to me. Without knowing your code architecture, I can only guess that you populate a widget or more widgets with your database content. You can just write a private method or a slot that clears the widget(s) and populates them. After your import, you call the private method or emit a signal connected to your slot. No need to close and restart your main window with the user loosing size and margins.

            C++ and Python walk into a bar. C++ reuses the first glass.

            SPlattenS 1 Reply Last reply
            1
            • jsulmJ jsulm

              @SPlatten said in Qt MainWindow how to close and reopen:

              What I want to do is trigger these actions automatically after the import completes so the dialog shows the data automatically.

              Well, I don't know your code, so can't say why it does not show the data in the dialog. You will need to show your code.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #6

              @jsulm , I import the file using the following:

                  //Show busy cursor
                          QApplication::setOverrideCursor(Qt::WaitCursor);
                  //Build import command line
                          QString strApp(DataSets::mscszMySQL);
                          QStringList slstArguments;
                          slstArguments << (QString(DataSets::mscszOptionHost)
                                                 + Trainer::strHost())
                                        << (QString(DataSets::mscszOptionUser)
                                                 + Trainer::strUser())
                                        << (QString(DataSets::mscszOptionPassword)
                                                 + Trainer::strPassword())
                                        << QString(DataSets::mscszOptionComments);
                          QProcess* pobjProc(new QProcess(this));
                          if ( pobjProc != nullptr )
                          {
                              QObject::connect(pobjProc, &QProcess::errorOccurred,
                                  [pobjProc](QProcess::ProcessError eError)
                                  {
                                      qdbg() << tr("Import error: ") << eError;
                                  }
                              );
                              strApp = strDBinstFolder + strApp;
                              pobjProc->setWorkingDirectory(strDBinstFolder);
                              pobjProc->setStandardInputFile(strImportFile);
                              pobjProc->start(strApp, slstArguments);
                              pobjProc->waitForFinished();
                          }
                  //Update the main dialog
                          emit closeAndReopen();
                  //Restore cursor
                          QApplication::restoreOverrideCursor();
              

              There are several issues here which I would like to resolve:

              1. No wait cursor is displayed, despite the call at the start.
              2. The line: emit closeAndReopen is connected to my own slot which I want to populate with the code to update the main window.

              Thats it, the dialog contains a table view which when the dialog is displayed automatically shows the database content. I want to trigger this stage automatically.

              Kind Regards,
              Sy

              jsulmJ 1 Reply Last reply
              0
              • AxelViennaA AxelVienna

                Closing and re-launching your entire main window in your case seems too much of the good thing to me. Without knowing your code architecture, I can only guess that you populate a widget or more widgets with your database content. You can just write a private method or a slot that clears the widget(s) and populates them. After your import, you call the private method or emit a signal connected to your slot. No need to close and restart your main window with the user loosing size and margins.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #7

                @AxelVienna Thats pretty much what I was thinking, except instead of adding additional code I want to trigger the same process that already occurs when the window is displayed.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @jsulm , I import the file using the following:

                      //Show busy cursor
                              QApplication::setOverrideCursor(Qt::WaitCursor);
                      //Build import command line
                              QString strApp(DataSets::mscszMySQL);
                              QStringList slstArguments;
                              slstArguments << (QString(DataSets::mscszOptionHost)
                                                     + Trainer::strHost())
                                            << (QString(DataSets::mscszOptionUser)
                                                     + Trainer::strUser())
                                            << (QString(DataSets::mscszOptionPassword)
                                                     + Trainer::strPassword())
                                            << QString(DataSets::mscszOptionComments);
                              QProcess* pobjProc(new QProcess(this));
                              if ( pobjProc != nullptr )
                              {
                                  QObject::connect(pobjProc, &QProcess::errorOccurred,
                                      [pobjProc](QProcess::ProcessError eError)
                                      {
                                          qdbg() << tr("Import error: ") << eError;
                                      }
                                  );
                                  strApp = strDBinstFolder + strApp;
                                  pobjProc->setWorkingDirectory(strDBinstFolder);
                                  pobjProc->setStandardInputFile(strImportFile);
                                  pobjProc->start(strApp, slstArguments);
                                  pobjProc->waitForFinished();
                              }
                      //Update the main dialog
                              emit closeAndReopen();
                      //Restore cursor
                              QApplication::restoreOverrideCursor();
                  

                  There are several issues here which I would like to resolve:

                  1. No wait cursor is displayed, despite the call at the start.
                  2. The line: emit closeAndReopen is connected to my own slot which I want to populate with the code to update the main window.

                  Thats it, the dialog contains a table view which when the dialog is displayed automatically shows the database content. I want to trigger this stage automatically.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @SPlatten said in Qt MainWindow how to close and reopen:

                  pobjProc->waitForFinished();

                  Why do you wait for process to finish? That blocks the event loop and is the reason why the cursor is not changed!

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  SPlattenS 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @SPlatten said in Qt MainWindow how to close and reopen:

                    pobjProc->waitForFinished();

                    Why do you wait for process to finish? That blocks the event loop and is the reason why the cursor is not changed!

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @jsulm , I just want the application to display a busy cursor until the process has completed.

                    Kind Regards,
                    Sy

                    jsulmJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @jsulm , I just want the application to display a busy cursor until the process has completed.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @SPlatten This does not answer my question. As I said: the reason why the cursor is not changed is the blocking pobjProc->waitForFinished() call...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      SPlattenS 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @SPlatten This does not answer my question. As I said: the reason why the cursor is not changed is the blocking pobjProc->waitForFinished() call...

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by SPlatten
                        #11

                        @jsulm I know that if I use HeidiSQL to import the 133Mb SQL file it doesn't complete instantly, when I do it in code the return is instant without waiting for the process which is occurring in the background to complete, that is why I put in the call to wait and want the wait cursor to be displayed so the use knows the process is doing something.

                        Kind Regards,
                        Sy

                        jsulmJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @jsulm I know that if I use HeidiSQL to import the 133Mb SQL file it doesn't complete instantly, when I do it in code the return is instant without waiting for the process which is occurring in the background to complete, that is why I put in the call to wait and want the wait cursor to be displayed so the use knows the process is doing something.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @SPlatten I really don't know how I should explain it better!
                          You set the cursor and then block Qt event loop, so you prevent Qt from actually changing the cursor or doing any other UI related stuff. This is very basic thing one needs to understand when using Qt.
                          Do not block event loop! So, do not wait for process to finish but use a slot connected to https://doc.qt.io/qt-5/qprocess.html#finished signal...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          SPlattenS 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @SPlatten I really don't know how I should explain it better!
                            You set the cursor and then block Qt event loop, so you prevent Qt from actually changing the cursor or doing any other UI related stuff. This is very basic thing one needs to understand when using Qt.
                            Do not block event loop! So, do not wait for process to finish but use a slot connected to https://doc.qt.io/qt-5/qprocess.html#finished signal...

                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by SPlatten
                            #13

                            @jsulm , ok, but as I said, it I remove the wait calls the process returns instantly and carries on in the background, no cursor is displayed, probably because there is no gap in the two cursors being displayed so is there a signal that I can connect to that I can use to restore the cursor when the process completes?

                            I'm going to try using the finished signal.

                            Kind Regards,
                            Sy

                            jsulmJ 1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @jsulm , ok, but as I said, it I remove the wait calls the process returns instantly and carries on in the background, no cursor is displayed, probably because there is no gap in the two cursors being displayed so is there a signal that I can connect to that I can use to restore the cursor when the process completes?

                              I'm going to try using the finished signal.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @SPlatten said in Qt MainWindow how to close and reopen:

                              no cursor is displayed

                              Move QApplication::restoreOverrideCursor() call to the slot connected to https://doc.qt.io/qt-5/qprocess.html#finished signal

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              2
                              • AxelViennaA Offline
                                AxelViennaA Offline
                                AxelVienna
                                wrote on last edited by
                                #15

                                I understand your motivation to wait for the import to finish. I propose to redesign your architecture a bit:

                                • Table view Population
                                  Encapsulate this in a separate method. You may want to add a bool parameter to tell it if the table view should or should not be cleared at the beginning.

                                • import
                                  Run this in the background so it does not block the event loop as jsulm says. Define a signal with a parameter to tell the slot if the import was successful so it knows if it has to repopulate.

                                • wait cursor
                                  Handle this at UI level, i.e. when the import button is pressed. Restore the cursor in the slot triggered by the import finished signal.

                                C++ and Python walk into a bar. C++ reuses the first glass.

                                SPlattenS 1 Reply Last reply
                                3
                                • AxelViennaA AxelVienna

                                  I understand your motivation to wait for the import to finish. I propose to redesign your architecture a bit:

                                  • Table view Population
                                    Encapsulate this in a separate method. You may want to add a bool parameter to tell it if the table view should or should not be cleared at the beginning.

                                  • import
                                    Run this in the background so it does not block the event loop as jsulm says. Define a signal with a parameter to tell the slot if the import was successful so it knows if it has to repopulate.

                                  • wait cursor
                                    Handle this at UI level, i.e. when the import button is pressed. Restore the cursor in the slot triggered by the import finished signal.

                                  SPlattenS Offline
                                  SPlattenS Offline
                                  SPlatten
                                  wrote on last edited by
                                  #16

                                  @AxelVienna , thank you, I will investigate.

                                  Kind Regards,
                                  Sy

                                  1 Reply Last reply
                                  0

                                  • Login

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