How do I declare a Qt signal so Resharper doesn't give me a warning?

840 Views Asked by At

If I have a class that declares a Qt signal:

class Test
{
signals:
    void TestSignal();
}

This works fine in Qt. But Resharper C++ gives me the warning:

Function 'void Test::TestSignal()' is not implemented

This is correct but pointless since Qt doesn't want the function to be implemented. Is there any way to implemented this signal so that Resharper doesn't complain?

I tried:

class Test
{
signals:
    void TestSignal() = {};
}

but get the build error:

error : Not a signal declaration

2

There are 2 best solutions below

1
Igor Akhmetov On BEST ANSWER

As AcerExtensa explained, the MOC tool generates implementations of Qt signals. To silence the warning, you can include the generated source files into your solution so that ReSharper would see implementations of signals in your header. You are right though that this warning is useless, I've filed https://youtrack.jetbrains.com/issue/RSCPP-20044 to silence the "Function is not implemented" inspection for Qt signals.

1
Xplatforms On

Qt doesn't want the function to be implemented

This statement is wrong. Qt is Framework and not Language. If you don't define your function you will get error from compiler.

Qt has tool called MOC (Meta Object Compiler). MOC is a code generator. It parses the header files and generates an additional C++ file that is compiled with the rest of the program. That generated C++ file contains all the information required for the introspection(signals/slots mechanism).

So to make your code work you need first of all include Q_OBJECT def in your class and your class should inherit from QObject or one of its subclasses (e.g., QWidget) so Qt can use it QMetaObject's. If your compiler or better to say your DevEnv don't know how to generate moc's files you need to do it yourself and then include moc file in your project source tree or include it directly at the end of your source file like:

#include "yoursourcefilename.moc"

You can read mor about Qt Meta Object system here: The Meta-Object System