Solved How to call operator()() as a slot?
-
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);
-
@SRaD Should be enough to capture "this" as "check" is a member variable:
connect(this, &MainWindow::startCheckTool, this, [this] () -> void { check->operator () (); });
-
You can't because your
CheckTool
isn't copyable as it's derived fromQObject
. You can wrap it, though:connect(this, &MainWindow::startCheckTool, check, [check] () -> void { check->operator () (); });
-
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. -
@SRaD Should be enough to capture "this" as "check" is a member variable:
connect(this, &MainWindow::startCheckTool, this, [this] () -> void { check->operator () (); });