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. Why QApplication::processEvents(QEventLoop::AllEvents, 20);?
Forum Updated to NodeBB v4.3 + New Features

Why QApplication::processEvents(QEventLoop::AllEvents, 20);?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.7k 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.
  • F Offline
    F Offline
    firen
    wrote on last edited by
    #1

    Hi,

    in some older code QCoreApplication::processEvents is used before a loop starts and always at the end of the loop. Inside the loop content of a derived version of a QTextEdit is rendered (the content could be a lot).

    I am not sure what the benefit of the using of processEvents is?

    Some collegue figgured out that the combination of processevents and the closing of a dialog could lead to a crash and i am thinking about if i could just delete the processEvents ... ? Aren't the events processed anyway?

    Thank you!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Rule of thumb: Do not use QCoreApplication::processEvents() at all. As you see it can lead to crashes because e.g. signals & slots are executed during this call. This means another signal is executed while you're still in a slot and this signal can change the state of the object you're currently working on (and also destroy it).

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

      F 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        Rule of thumb: Do not use QCoreApplication::processEvents() at all. As you see it can lead to crashes because e.g. signals & slots are executed during this call. This means another signal is executed while you're still in a slot and this signal can change the state of the object you're currently working on (and also destroy it).

        F Offline
        F Offline
        firen
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks! So what is the intendet purpose of it? Do I understand

        The event loop is started with a call to exec(). Long-running operations can call processEvents() to keep the application responsive.
        

        right, that for example if I save for example a big file, the GUI would still response to interaction and not "blocked"?

        JonBJ 1 Reply Last reply
        0
        • F firen

          @Christian-Ehrlicher Thanks! So what is the intendet purpose of it? Do I understand

          The event loop is started with a call to exec(). Long-running operations can call processEvents() to keep the application responsive.
          

          right, that for example if I save for example a big file, the GUI would still response to interaction and not "blocked"?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @firen
          You could call processEvents() if you are looping saving a block at a time to file. That would keep the UI responsive, but then you must be prepared to handle that your UI code may go off doing something while you are still in a slot saving a file.

          A better way for saving a large file would probably be to do it in a thread.

          As @Christian-Ehrlicher says the rule of thumb is to try to avoid processEvents(), but it's not forbidden.

          F 1 Reply Last reply
          2
          • JonBJ JonB

            @firen
            You could call processEvents() if you are looping saving a block at a time to file. That would keep the UI responsive, but then you must be prepared to handle that your UI code may go off doing something while you are still in a slot saving a file.

            A better way for saving a large file would probably be to do it in a thread.

            As @Christian-Ehrlicher says the rule of thumb is to try to avoid processEvents(), but it's not forbidden.

            F Offline
            F Offline
            firen
            wrote on last edited by
            #5

            @JonB Thank you for clarification

            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