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. QFuture problem
Forum Updated to NodeBB v4.3 + New Features

QFuture problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 700 Views 1 Watching
  • 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello!

    I am using a QFuture to call a function that obtain the size of a directory. This function spend a lot of time and is called when I open a window so, in order to not blocking the window, I want to call it with a QFuture.

    The problem is, when I close the window before the QFuture is finished and I try to open the window again, I obtain a memory access error because I think the QFuture finish without the window. It tries to put the value on the label and this is the error.

    Code:

    CleanWindow::CleanWindow(CConfig *pConfig, QWidget *parent) :
        QDialog(parent),
        m_pConfig(pConfig),
        ui(new Ui::CleanWindow)
    {
        ui->setupUi(this);
        setWindowTitle("Clean window");
        setMinimumSize(1000, 650);
        setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
       
        QFuture<void> future = QtConcurrent::run([=]() {
            obtainSize();
        });
    }
    
    CleanWindow::~CleanWindow()
    {
        delete ui;
    }
    
    void CleanWindow::obtainSize() {
        ui->labelFolderSizeValue->setText(CUtil::getFormatSize(CUtil::getDirSize(m_pConfig->getConfigGeneral()->getDirVideo())));
    }
    

    How can I delete or disable the QFuture when I close the window?

    Thank you very much!

    J.HilkJ beeckscheB 2 Replies Last reply
    0
    • ivanicyI ivanicy

      Hello!

      I am using a QFuture to call a function that obtain the size of a directory. This function spend a lot of time and is called when I open a window so, in order to not blocking the window, I want to call it with a QFuture.

      The problem is, when I close the window before the QFuture is finished and I try to open the window again, I obtain a memory access error because I think the QFuture finish without the window. It tries to put the value on the label and this is the error.

      Code:

      CleanWindow::CleanWindow(CConfig *pConfig, QWidget *parent) :
          QDialog(parent),
          m_pConfig(pConfig),
          ui(new Ui::CleanWindow)
      {
          ui->setupUi(this);
          setWindowTitle("Clean window");
          setMinimumSize(1000, 650);
          setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
         
          QFuture<void> future = QtConcurrent::run([=]() {
              obtainSize();
          });
      }
      
      CleanWindow::~CleanWindow()
      {
          delete ui;
      }
      
      void CleanWindow::obtainSize() {
          ui->labelFolderSizeValue->setText(CUtil::getFormatSize(CUtil::getDirSize(m_pConfig->getConfigGeneral()->getDirVideo())));
      }
      

      How can I delete or disable the QFuture when I close the window?

      Thank you very much!

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

      hi @ivanicy

      the short answer is, you can't

      The longer explanation:
      QFuture is only meant to query the QtConcurrent thread, of its state, running/finished etc

      https://doc.qt.io/qt-5/qtconcurrent.html#run-1

      Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.

      You can however, set an atomic bool, that is check inside run and exit that way.

      Also your QFuture is local, and gets destroyed as soon as the constructor finished, regardless of the QtConcurrent running or not.


      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
      2
      • ivanicyI ivanicy

        Hello!

        I am using a QFuture to call a function that obtain the size of a directory. This function spend a lot of time and is called when I open a window so, in order to not blocking the window, I want to call it with a QFuture.

        The problem is, when I close the window before the QFuture is finished and I try to open the window again, I obtain a memory access error because I think the QFuture finish without the window. It tries to put the value on the label and this is the error.

        Code:

        CleanWindow::CleanWindow(CConfig *pConfig, QWidget *parent) :
            QDialog(parent),
            m_pConfig(pConfig),
            ui(new Ui::CleanWindow)
        {
            ui->setupUi(this);
            setWindowTitle("Clean window");
            setMinimumSize(1000, 650);
            setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
           
            QFuture<void> future = QtConcurrent::run([=]() {
                obtainSize();
            });
        }
        
        CleanWindow::~CleanWindow()
        {
            delete ui;
        }
        
        void CleanWindow::obtainSize() {
            ui->labelFolderSizeValue->setText(CUtil::getFormatSize(CUtil::getDirSize(m_pConfig->getConfigGeneral()->getDirVideo())));
        }
        

        How can I delete or disable the QFuture when I close the window?

        Thank you very much!

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by
        #3

        @ivanicy

        You could use the https://doc.qt.io/qt-5/qfuturesynchronizer.html and add the future. When the QFutureSynchronizer is destroyed, it will be wait until all futures are finished.

        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