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. How to stop a method while it uses QWait()
Forum Updated to NodeBB v4.3 + New Features

How to stop a method while it uses QWait()

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

    Hi,
    I have a program that visualizes some sorting algorithms:

    0_1548924529571_Screenshot from 2019-01-30 14-30-11.png

    To draw this set of bars it uses two classes - Array and Painter.
    Array contains actual sorting methods that, after any change in the list being sorted, sleep for some time with QWait() function and emit signal list_changed, connected to a draw() slot in Painter class.

    My problem is that Array is still sorting the list after I close the main window, and the program won't quit until the sorting is complete. I also want the stop button I put in the top left corner to be able to quit the sorting method.
    I know that I need to use QThread class somehow and put Array into it but I have no knowledge about how it works (I'm pretty new to qt). I also suspect that QWait() function is probably a wrong choice for this task, but it's so easy to use and I don't know about any alternatives.

    Does anybody have any advice?
    Thanks in advance!

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

      Don't use qWait() - it's not meant to be used in UI related code. Maybe even not in a separate QThread.

      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
      • O ozymohliad

        Hi,
        I have a program that visualizes some sorting algorithms:

        0_1548924529571_Screenshot from 2019-01-30 14-30-11.png

        To draw this set of bars it uses two classes - Array and Painter.
        Array contains actual sorting methods that, after any change in the list being sorted, sleep for some time with QWait() function and emit signal list_changed, connected to a draw() slot in Painter class.

        My problem is that Array is still sorting the list after I close the main window, and the program won't quit until the sorting is complete. I also want the stop button I put in the top left corner to be able to quit the sorting method.
        I know that I need to use QThread class somehow and put Array into it but I have no knowledge about how it works (I'm pretty new to qt). I also suspect that QWait() function is probably a wrong choice for this task, but it's so easy to use and I don't know about any alternatives.

        Does anybody have any advice?
        Thanks in advance!

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @ozymohliad said in How to stop a method while it uses QWait():

        sleep for some time with QWait()

        Better use a QTimer for that. You can simply cancel it and then the timeout signal will not be emitted.

        Also, you do not block your program.

        Multi-threading is possible too, but too complicated for your case, I think

        Qt has to stay free or it will die.

        1 Reply Last reply
        2

        • Login

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