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. Black screen when a loop is in a code
Forum Updated to NodeBB v4.3 + New Features

Black screen when a loop is in a code

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 602 Views
  • 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.
  • A Offline
    A Offline
    answermeplz
    wrote on 2 Mar 2024, 20:43 last edited by answermeplz 3 Feb 2024, 20:49
    #1

    I am new to QT.
    Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
    When loop is commented out, the GUI will show up.
    Where is problem?

    EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        for (int i = 0; i <10; i++){
            printf("hello");
    
        }
       ////////////OR
        while (true){
             print("hello\n");  
       }
    }
    
    J C C 3 Replies Last reply 2 Mar 2024, 20:46
    0
    • A answermeplz
      2 Mar 2024, 20:43

      I am new to QT.
      Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
      When loop is commented out, the GUI will show up.
      Where is problem?

      EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          for (int i = 0; i <10; i++){
              printf("hello");
      
          }
         ////////////OR
          while (true){
               print("hello\n");  
         }
      }
      
      J Offline
      J Offline
      JoeCFD
      wrote on 2 Mar 2024, 20:46 last edited by
      #2

      @answermeplz said in Black screen when a loop is in a code:

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
      *for (int i = 0; i <10; i++){
      printf("hello");

      }*
      

      OR
      while (true){
      print("hello\n");
      }

      }

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          reurn a.exec();
      }
      
      A 1 Reply Last reply 2 Mar 2024, 20:54
      0
      • J JoeCFD
        2 Mar 2024, 20:46

        @answermeplz said in Black screen when a loop is in a code:

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        *for (int i = 0; i <10; i++){
        printf("hello");

        }*
        

        OR
        while (true){
        print("hello\n");
        }

        }

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
            reurn a.exec();
        }
        
        A Offline
        A Offline
        answermeplz
        wrote on 2 Mar 2024, 20:54 last edited by
        #3

        @JoeCFD I dont understand

        J 1 Reply Last reply 2 Mar 2024, 20:56
        0
        • A answermeplz
          2 Mar 2024, 20:54

          @JoeCFD I dont understand

          J Offline
          J Offline
          JoeCFD
          wrote on 2 Mar 2024, 20:56 last edited by JoeCFD 3 Feb 2024, 20:57
          #4

          @answermeplz
          you need to call the following:
          a.exec();

          read here:
          https://doc.qt.io/qt-5/qapplication.html#exec

          1 Reply Last reply
          0
          • A Offline
            A Offline
            answermeplz
            wrote on 2 Mar 2024, 21:08 last edited by answermeplz 3 Feb 2024, 21:09
            #5

            @answermeplz Sure, I call a.exec, I just forgot to put it here.

            1 Reply Last reply
            0
            • A answermeplz
              2 Mar 2024, 20:43

              I am new to QT.
              Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
              When loop is commented out, the GUI will show up.
              Where is problem?

              EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
                  for (int i = 0; i <10; i++){
                      printf("hello");
              
                  }
                 ////////////OR
                  while (true){
                       print("hello\n");  
                 }
              }
              
              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 2 Mar 2024, 21:44 last edited by
              #6

              @answermeplz said in Black screen when a loop is in a code:

              GUI will show up after the loop has ended, but I need to run it simultaneously.

              This will and can't work since for the Qt stuff to work you need a running event loop. Put your work into a separate worker thread and communicate to/from them with signals and slots.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • A answermeplz
                2 Mar 2024, 20:43

                I am new to QT.
                Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
                When loop is commented out, the GUI will show up.
                Where is problem?

                EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    MainWindow w;
                    w.show();
                    for (int i = 0; i <10; i++){
                        printf("hello");
                
                    }
                   ////////////OR
                    while (true){
                         print("hello\n");  
                   }
                }
                
                C Offline
                C Offline
                ChrisW67
                wrote on 2 Mar 2024, 22:22 last edited by ChrisW67 3 Feb 2024, 22:23
                #7

                @answermeplz said in Black screen when a loop is in a code:

                Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
                When loop is commented out, the GUI will show up.
                Where is problem?

                For any of Qt's events to operate the main (GUI) thread needs to be executing the Qt event loop whenever it is not actively responding to one of those events. In your code (that I have added a call to QAppplication::exec() in):

                int main(int argc, char *argv[])
                {
                   QApplication a(argc, argv);
                   MainWindow w;
                   w.show();
                

                So far, so good. This bit,

                    for (int i = 0; i <10; i++){
                      printf("hello");
                    }
                

                will run until completion in a matter of milliseconds and drop through to:

                 a.exec();
                }
                

                and your UI will display.

                However, if you use this code,

                    while (true){
                       print("hello\n");  
                    }
                

                this will never terminate, never fall through to the Qt event loop, and thus never finish displaying the Qt GUI or start responding to user inputs.

                EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.

                Simultaneously with what?

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  answermeplz
                  wrote on 3 Mar 2024, 12:16 last edited by answermeplz 3 Mar 2024, 12:18
                  #8

                  Okay, I understand, thank you very much!
                  I work on Raspberry Pi project, and my goal is to have one thread handling GUI and second thread handling measuring and processing data from sensor.
                  My goal is to run GUI simultaneously with measuring of data, while GUI is not blocked because of infinite loop.
                  So correct way would be for example like this?:

                  void sensorfunction(){
                      while(true){
                          //processing data -> sending them to GUI
                          std::this_thread::sleep_for(std::chrono::seconds(1));
                      }
                  }
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                      std::thread thread1(sensorfunction);
                  
                  }
                  
                  
                  JonBJ Ronel_qtmasterR 2 Replies Last reply 3 Mar 2024, 12:23
                  0
                  • A answermeplz
                    3 Mar 2024, 12:16

                    Okay, I understand, thank you very much!
                    I work on Raspberry Pi project, and my goal is to have one thread handling GUI and second thread handling measuring and processing data from sensor.
                    My goal is to run GUI simultaneously with measuring of data, while GUI is not blocked because of infinite loop.
                    So correct way would be for example like this?:

                    void sensorfunction(){
                        while(true){
                            //processing data -> sending them to GUI
                            std::this_thread::sleep_for(std::chrono::seconds(1));
                        }
                    }
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MainWindow w;
                        w.show();
                    
                        return a.exec();
                        std::thread thread1(sensorfunction);
                    
                    }
                    
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 3 Mar 2024, 12:23 last edited by JonB 3 Mar 2024, 12:25
                    #9

                    @answermeplz
                    No, because you won't even hit std::thread thread1(sensorfunction); statement since it's after a return statement! (Compiler might have warned you of this "unreachable statement"?)

                    In some shape of form, you have to kick off the thread before entering a.exec() or from within it. Use Qt's threading functions. have a read of e.g. https://wiki.qt.io/QThreads_general_usage for "worker class" pattern. If the thread needs to communicate with the main UI (or opposite direction) thread use signals/slots to communicate/pass data, do not allow worker thread to access UI directly.

                    1 Reply Last reply
                    0
                    • A answermeplz
                      3 Mar 2024, 12:16

                      Okay, I understand, thank you very much!
                      I work on Raspberry Pi project, and my goal is to have one thread handling GUI and second thread handling measuring and processing data from sensor.
                      My goal is to run GUI simultaneously with measuring of data, while GUI is not blocked because of infinite loop.
                      So correct way would be for example like this?:

                      void sensorfunction(){
                          while(true){
                              //processing data -> sending them to GUI
                              std::this_thread::sleep_for(std::chrono::seconds(1));
                          }
                      }
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          MainWindow w;
                          w.show();
                      
                          return a.exec();
                          std::thread thread1(sensorfunction);
                      
                      }
                      
                      
                      Ronel_qtmasterR Offline
                      Ronel_qtmasterR Offline
                      Ronel_qtmaster
                      wrote on 3 Mar 2024, 12:31 last edited by Ronel_qtmaster 3 Mar 2024, 12:32
                      #10

                      @answermeplz The same thread for the gui can support sensor reading, however you will not use while loop, but check for readyRead signal of QSerialPort when the port is opened, process the data.
                      check this https://doc.qt.io/qt-6/qtserialport-terminal-example.html

                      1 Reply Last reply
                      1

                      1/10

                      2 Mar 2024, 20:43

                      • Login

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