How can I call a method from QMainWindow?

337 views Asked by At

I do need to invoke a method from my MainWindow class, that inherit from QMainWindow class from a class outside MainWindow, something like this:

Q_ASSERT(QMetaObject::invokeMethod(mainWindow, "attachmentDownloadComplete"));

mainWindow is of class MainWindow : public QMainWindow type

the error is:

no matching function for call to 'QMetaObject::invokeMethod(MainWindow*&, const char [27])'
     Q_ASSERT(QMetaObject::invokeMethod(mainWindow, "attachmentDownloadComplete"));

My question is how can I manage to call invoke this method?

1

There are 1 answers

0
Pavan Chandaka On

The problem is, the slot you are trying to invoke has an input argument.

When you have an input argument you need to specify the argument with Q_ARG.

ex:

In my main window I have a slot, as shown below.

public slots:

    void doSomeTest(const char* name) { std::cout << "testing Something"; }

In some other file when invoking it, should do as said below. Tested and working as expected.

QMetaObject::invokeMethod(mainWin, "doSomeTest", Q_ARG(const char*, "test test"));

First parameter - Your main window object.

Second parameter - The slot name.

From third on all your input parameters with types.

If you have any return type use

Q_RETURN_ARG(RETURN_TYPE, RETURN_VALUE_PARAMETER),