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 603 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 last edited by answermeplz
    #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");  
       }
    }
    
    JoeCFDJ Christian EhrlicherC C 3 Replies Last reply
    0
    • A answermeplz

      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");  
         }
      }
      
      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      0
      • JoeCFDJ JoeCFD

        @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 last edited by
        #3

        @JoeCFD I dont understand

        JoeCFDJ 1 Reply Last reply
        0
        • A answermeplz

          @JoeCFD I dont understand

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #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 last edited by answermeplz
            #5

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

            1 Reply Last reply
            0
            • A answermeplz

              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");  
                 }
              }
              
              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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

                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 last edited by ChrisW67
                #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 last edited by answermeplz
                  #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
                  0
                  • A answermeplz

                    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 Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #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

                      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 last edited by Ronel_qtmaster
                      #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

                      • Login

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