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 call operator()() as a slot?
Forum Update on Monday, May 27th 2025

How to call operator()() as a slot?

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

    I start a worker thread like so ...

    {
        check = new CheckTool(args);
        check->moveToThread(&checkThread);
    
        connect(&checkThread, &QThread::finished, analyses,
            &QObject::deleteLater);
        connect(this, &MainWindow::startCheckTool, check,
            &CheckTool::doWork);
        connect(check, &CheckTool::notifyCheckToolFinished, this,
            &MainWindow::checkToolFinished);
    
        checkThread.start();
    } 
    

    but I have

    void CheckTool::operator()() { ... }
    

    defined as a CheckTool function. How can I call this in the following connect instead of using &CheckTool::doWork?

    connect(this, &MainWindow::startCheckTool, check,
        &CheckTool::doWork);
    
    kshegunovK 1 Reply Last reply
    0
    • S SRaD

      I tried that and got the following error.

      main-window.cpp: In member function ‘void MainWindow::setupCheckTool()’:
      main-window.cpp:448:4: error: capture of non-variable ‘MainWindow::check’
        448 |   [check] () -> void {check->operator()(); });
            |    ^~~~~~~~
      In file included from main-window.cpp:8:
      main-window.h:72:17: note: ‘CheckTool* MainWindow::check’ declared here
        72 |   CheckTool *check;
              |           ^~~~~~~~
      main-window.cpp: In lambda function:
      main-window.cpp:448:26: error: ‘this’ was not captured for this lambda function
        448 |   [check] () -> void {check->operator()(); });
                |                   ^~~~~~~~
      main-window.cpp:448:26: error: invalid use of non-static data member ‘MainWindow::check’
      In file included from main-window.cpp:8:
      main-window.h:72:17: note: declared here
         72 |   CheckTool *check;
        |              ^~~~~~~~
      

      I don't know why it seems to think check is a member function. CheckTool *check; is a private member of class MainWindow.

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

      @SRaD Should be enough to capture "this" as "check" is a member variable:

      connect(this, &MainWindow::startCheckTool, this, [this] () -> void { check->operator () (); });
      

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

      1 Reply Last reply
      1
      • S SRaD

        I start a worker thread like so ...

        {
            check = new CheckTool(args);
            check->moveToThread(&checkThread);
        
            connect(&checkThread, &QThread::finished, analyses,
                &QObject::deleteLater);
            connect(this, &MainWindow::startCheckTool, check,
                &CheckTool::doWork);
            connect(check, &CheckTool::notifyCheckToolFinished, this,
                &MainWindow::checkToolFinished);
        
            checkThread.start();
        } 
        

        but I have

        void CheckTool::operator()() { ... }
        

        defined as a CheckTool function. How can I call this in the following connect instead of using &CheckTool::doWork?

        connect(this, &MainWindow::startCheckTool, check,
            &CheckTool::doWork);
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #2

        You can't because your CheckTool isn't copyable as it's derived from QObject. You can wrap it, though:

        connect(this, &MainWindow::startCheckTool, check, [check] () -> void { check->operator () (); });
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SRaD
          wrote on last edited by SRaD
          #3

          I tried that and got the following error.

          main-window.cpp: In member function ‘void MainWindow::setupCheckTool()’:
          main-window.cpp:448:4: error: capture of non-variable ‘MainWindow::check’
            448 |   [check] () -> void {check->operator()(); });
                |    ^~~~~~~~
          In file included from main-window.cpp:8:
          main-window.h:72:17: note: ‘CheckTool* MainWindow::check’ declared here
            72 |   CheckTool *check;
                  |           ^~~~~~~~
          main-window.cpp: In lambda function:
          main-window.cpp:448:26: error: ‘this’ was not captured for this lambda function
            448 |   [check] () -> void {check->operator()(); });
                    |                   ^~~~~~~~
          main-window.cpp:448:26: error: invalid use of non-static data member ‘MainWindow::check’
          In file included from main-window.cpp:8:
          main-window.h:72:17: note: declared here
             72 |   CheckTool *check;
            |              ^~~~~~~~
          

          I don't know why it seems to think check is a member function. CheckTool *check; is a private member of class MainWindow.

          jsulmJ 1 Reply Last reply
          0
          • S SRaD

            I tried that and got the following error.

            main-window.cpp: In member function ‘void MainWindow::setupCheckTool()’:
            main-window.cpp:448:4: error: capture of non-variable ‘MainWindow::check’
              448 |   [check] () -> void {check->operator()(); });
                  |    ^~~~~~~~
            In file included from main-window.cpp:8:
            main-window.h:72:17: note: ‘CheckTool* MainWindow::check’ declared here
              72 |   CheckTool *check;
                    |           ^~~~~~~~
            main-window.cpp: In lambda function:
            main-window.cpp:448:26: error: ‘this’ was not captured for this lambda function
              448 |   [check] () -> void {check->operator()(); });
                      |                   ^~~~~~~~
            main-window.cpp:448:26: error: invalid use of non-static data member ‘MainWindow::check’
            In file included from main-window.cpp:8:
            main-window.h:72:17: note: declared here
               72 |   CheckTool *check;
              |              ^~~~~~~~
            

            I don't know why it seems to think check is a member function. CheckTool *check; is a private member of class MainWindow.

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

            @SRaD Should be enough to capture "this" as "check" is a member variable:

            connect(this, &MainWindow::startCheckTool, this, [this] () -> void { check->operator () (); });
            

            https://forum.qt.io/topic/113070/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