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. My Qt Window is crashing while running a C++ loop

My Qt Window is crashing while running a C++ loop

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 682 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.
  • YunusY Offline
    YunusY Offline
    Yunus
    wrote on last edited by Yunus
    #1

    Hi all, I have developed a qml interface with some buttons. For example my button is:

    Button {
            text: "RUN"
            onClicked: 
              {
                   my_func.run()
             }
        }
    

    And when this is clicked, this will execute a cpp function. And my cpp funtion is:

    int my_func::run(){
        int counter =0;
        while(1)
        {
              counter++;
              if(counter>1000000)
                    break;
        }
    }
    

    When I clicked the button, after a while my interface is crashing in the background. Why is this happening.

    jsulmJ 1 Reply Last reply
    0
    • YunusY Yunus

      Hi all, I have developed a qml interface with some buttons. For example my button is:

      Button {
              text: "RUN"
              onClicked: 
                {
                     my_func.run()
               }
          }
      

      And when this is clicked, this will execute a cpp function. And my cpp funtion is:

      int my_func::run(){
          int counter =0;
          while(1)
          {
                counter++;
                if(counter>1000000)
                      break;
          }
      }
      

      When I clicked the button, after a while my interface is crashing in the background. Why is this happening.

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

      @yunus said in My Qt Window is crashing while running a C++ loop:

      my interface is crashing in the background

      What do you mean by that?
      Are you aware that such a loop is blocking your UI as long as it is running? You should not do such things in event driven applications.

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

      1 Reply Last reply
      3
      • YunusY Offline
        YunusY Offline
        Yunus
        wrote on last edited by
        #3

        @jsulm Thanks for ur reply. I mean my window's color is changing badly. But if that loop occurs some problem. Cant I run a loop in C++ part?

        jsulmJ 1 Reply Last reply
        0
        • YunusY Offline
          YunusY Offline
          Yunus
          wrote on last edited by
          #4

          When I added to the loop: Below part simple opencv funtions. This while loop is working fine. I m really wondering why this is working without crashing?

          int my_func::run(){
          
           VideoCapture cap(0);
              Mat frame;
              while(1)
              {
                  cap>>frame;
                  imshow("asas",frame);
                
                
                  if(waitKey(1)==27){
                      break;
                  }
              }
          }
          
          1 Reply Last reply
          0
          • YunusY Yunus

            @jsulm Thanks for ur reply. I mean my window's color is changing badly. But if that loop occurs some problem. Cant I run a loop in C++ part?

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

            @yunus said in My Qt Window is crashing while running a C++ loop:

            I mean my window's color is changing badly

            Probably the OS is doing this because it thinks that your app is hanging (what it really does).
            Long lasting operations like that should be done in another thread, not UI thread.
            Or you use asynchronous (non blocking APIs) APIs like what Qt provides.

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

            1 Reply Last reply
            3
            • YunusY Offline
              YunusY Offline
              Yunus
              wrote on last edited by
              #6

              @jsulm okey, I will try to do in another thread. Is this good solution for my problem and is there a simple example about threads since I m new in Qt

              J.HilkJ jsulmJ 2 Replies Last reply
              0
              • YunusY Yunus

                @jsulm okey, I will try to do in another thread. Is this good solution for my problem and is there a simple example about threads since I m new in Qt

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @yunus
                take a look at the detailed description of the Thread class it has a working example in it:
                https://doc.qt.io/qt-5/qthread.html#details


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • YunusY Yunus

                  @jsulm okey, I will try to do in another thread. Is this good solution for my problem and is there a simple example about threads since I m new in Qt

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

                  @yunus Also this should be helpful: https://doc.qt.io/qt-5/thread-basics.html

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

                  1 Reply Last reply
                  3
                  • YunusY Offline
                    YunusY Offline
                    Yunus
                    wrote on last edited by
                    #9

                    @J-Hilk @jsulm Thanks for ur replies. I will try it if it is the only way.

                    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