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. Multi-threading design along with an undo/redo stack
Forum Updated to NodeBB v4.3 + New Features

Multi-threading design along with an undo/redo stack

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 221 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.
  • M Offline
    M Offline
    m3g1dd
    wrote on last edited by
    #1

    I have this call stack to perform a heavy computation:

    // QML
    
    StyledButton {
        onButtonClicked: {
            registeredCppClass.undoHandler.createCommand()
        }
    }
    
    void UndoHandler::createCommand()
    {
        m_undoStack->push(new Command());
    }
    
    class Command : public QUndoCommand
    {
    public:
        Command();
        virtual ~Command();
    
        virtual void undo();
        virtual void redo();
        
        // ...
    private:
        // Handler does the logic
        LogicHandler *m_logicHandler;
        // Output by logic handler
        QString m_outputName;
    };
    
    void Command::redo()
    {
        
        if (/* */) {
            
        } else {
            // Run heavy computation
            m_outputName = m_logicHandler->run();
        }
    }
    
    QString LogicHandler::run()
    {
        // Heavy computation starts
    }
    

    Intention

    I intend to implement QThread by this approach to prevent GUI from getting non-responsive while heavy computation is being done. But I don't know where QThread and Worker class need to be implemented. Should they be within:

    • UndoHandler::createCommand
    • Command::redo
    • LogicHandler::run
    • ... ?

    What is the best location for QThread and Worker considering their signal-slot connections?

    kshegunovK 1 Reply Last reply
    0
    • M m3g1dd

      I have this call stack to perform a heavy computation:

      // QML
      
      StyledButton {
          onButtonClicked: {
              registeredCppClass.undoHandler.createCommand()
          }
      }
      
      void UndoHandler::createCommand()
      {
          m_undoStack->push(new Command());
      }
      
      class Command : public QUndoCommand
      {
      public:
          Command();
          virtual ~Command();
      
          virtual void undo();
          virtual void redo();
          
          // ...
      private:
          // Handler does the logic
          LogicHandler *m_logicHandler;
          // Output by logic handler
          QString m_outputName;
      };
      
      void Command::redo()
      {
          
          if (/* */) {
              
          } else {
              // Run heavy computation
              m_outputName = m_logicHandler->run();
          }
      }
      
      QString LogicHandler::run()
      {
          // Heavy computation starts
      }
      

      Intention

      I intend to implement QThread by this approach to prevent GUI from getting non-responsive while heavy computation is being done. But I don't know where QThread and Worker class need to be implemented. Should they be within:

      • UndoHandler::createCommand
      • Command::redo
      • LogicHandler::run
      • ... ?

      What is the best location for QThread and Worker considering their signal-slot connections?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @m3g1dd said in Multi-threading design along with an undo/redo stack:

      What is the best location for QThread and Worker considering their signal-slot connections?

      QThread goes into an object in the controlling thread (usually the GUI thread), while the Worker is usually "free", that is I ordinarily don't keep a reference to it anywhere. Still depends on the actual logic, though, you could wrap a "job" based system, or migrate to QtConcurrent::run depending on the actual situation. Typically if you want to break the processing within a secondary thread (a worker one), you'd connect the interruption method (Qt::DirectConnection) to raise some atomic flag, which is checked on each iteration/atom of code that's executed and exit early when needed.

      Read and abide by the Qt Code of Conduct

      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