const member function can't emit a signal that is not const
-
UPDATE 1
It is by design
https://github.com/KDE/clazy/blob/master/docs/checks/README-const-signal-or-slot.md
Env
OS: Linux
Qt: 6.3.0
gcc: 12.1.0Problem
class A : public QObject { Q_OBJECT ... Q_SIGNALS: void Log(const LogItem & log_item); public: void TestSignal() const { emit Log(...)); }
A::TestSignal
leads to a build errorerror: passing ‘const A as ‘this’ argument discards qualifiers [-fpermissive]
If make the signal as const
void Log(const LogItem & log_item) const;
The build passes, but there is a warning for the signal
signal A::Log shouldn't be const [clazy-const-signal-or-slot]
Does this warning make sense?
Any better solution? -
@JonB said in const member function can't emit a signal that is not const:
Since signals change state, TestSignal should not be const.
In the view of an API user, emitting a signal should not change anything, so it should be
const
natively.
For this restriction, I have to do log in another way instead of by signals.@jronald said in const member function can't emit a signal that is not const:
In the view of an API user, emitting a signal should not change anything, so it should be const natively.
That is just something you are choosing to state. Signals do change object state, as per your link, else you would not get the error/warning.
For this restriction, I have to do log in another way instead of by signals.
Don't emit a signal from a
const
method, as the link states. Why does aconst
method need to emit a signal anyway?Or rather than abandon the logic of signals/slots, find a way to achieve your goal. E.g. maybe you could suppress warning "clazy-const-signal-or-slot" if you are determined to do things your current way.
I have not looked, but if you Google for something like
qt signal const
I would imagine if others have the same intention as you there will be hits.... -
UPDATE 1
It is by design
https://github.com/KDE/clazy/blob/master/docs/checks/README-const-signal-or-slot.md
Env
OS: Linux
Qt: 6.3.0
gcc: 12.1.0Problem
class A : public QObject { Q_OBJECT ... Q_SIGNALS: void Log(const LogItem & log_item); public: void TestSignal() const { emit Log(...)); }
A::TestSignal
leads to a build errorerror: passing ‘const A as ‘this’ argument discards qualifiers [-fpermissive]
If make the signal as const
void Log(const LogItem & log_item) const;
The build passes, but there is a warning for the signal
signal A::Log shouldn't be const [clazy-const-signal-or-slot]
Does this warning make sense?
Any better solution?@jronald said in const member function can't emit a signal that is not const:
A::TestSignal leads to a build error
As your lionk states:
Prevents you from emitting signals from const methods, as these methods shouldn't change state, and a signal implies state was changed
Since signals change state,
TestSignal
should not beconst
. -
@jronald said in const member function can't emit a signal that is not const:
A::TestSignal leads to a build error
As your lionk states:
Prevents you from emitting signals from const methods, as these methods shouldn't change state, and a signal implies state was changed
Since signals change state,
TestSignal
should not beconst
.@JonB said in const member function can't emit a signal that is not const:
Since signals change state, TestSignal should not be const.
In the view of an API user, emitting a signal should not change anything, so it should be
const
natively.
For this restriction, I have to do log in another way instead of by signals. -
@JonB said in const member function can't emit a signal that is not const:
Since signals change state, TestSignal should not be const.
In the view of an API user, emitting a signal should not change anything, so it should be
const
natively.
For this restriction, I have to do log in another way instead of by signals.@jronald said in const member function can't emit a signal that is not const:
In the view of an API user, emitting a signal should not change anything, so it should be const natively.
That is just something you are choosing to state. Signals do change object state, as per your link, else you would not get the error/warning.
For this restriction, I have to do log in another way instead of by signals.
Don't emit a signal from a
const
method, as the link states. Why does aconst
method need to emit a signal anyway?Or rather than abandon the logic of signals/slots, find a way to achieve your goal. E.g. maybe you could suppress warning "clazy-const-signal-or-slot" if you are determined to do things your current way.
I have not looked, but if you Google for something like
qt signal const
I would imagine if others have the same intention as you there will be hits.... -
@jronald said in const member function can't emit a signal that is not const:
In the view of an API user, emitting a signal should not change anything, so it should be const natively.
That is just something you are choosing to state. Signals do change object state, as per your link, else you would not get the error/warning.
For this restriction, I have to do log in another way instead of by signals.
Don't emit a signal from a
const
method, as the link states. Why does aconst
method need to emit a signal anyway?Or rather than abandon the logic of signals/slots, find a way to achieve your goal. E.g. maybe you could suppress warning "clazy-const-signal-or-slot" if you are determined to do things your current way.
I have not looked, but if you Google for something like
qt signal const
I would imagine if others have the same intention as you there will be hits....@JonB said in const member function can't emit a signal that is not const:
Why does a const method need to emit a signal anyway?
Emitting a signal still keeps the method constant.
If it is allowed I'll do it, but it isn't allowed now, so I need to find another way.Thanks
-
@JonB said in const member function can't emit a signal that is not const:
Why does a const method need to emit a signal anyway?
Emitting a signal still keeps the method constant.
If it is allowed I'll do it, but it isn't allowed now, so I need to find another way.Thanks
@jronald said in const member function can't emit a signal that is not const:
Emitting a signal still keeps the method constant.
But it does not, since a signal method is not
const
, as per the error message/debate we are having.Anyway the question was why does a method which is
const
need to emit a signal anyway? If nothing is being altered (const
method) what is it emitting a signal for? -
@jronald said in const member function can't emit a signal that is not const:
Emitting a signal still keeps the method constant.
But it does not, since a signal method is not
const
, as per the error message/debate we are having.Anyway the question was why does a method which is
const
need to emit a signal anyway? If nothing is being altered (const
method) what is it emitting a signal for?@JonB said in const member function can't emit a signal that is not const:
If nothing is being altered (const method) what is it emitting a signal for?
In my app, the const method is to parse data from a pdf file, when it fails, it's helpful to see the logs, and GUI is more powerful to show the logs. If signals are emitted at key points in the method, the signals can be connected to a widgets,then the logs could be shown instantly and nicely.
-
@JonB said in const member function can't emit a signal that is not const:
If nothing is being altered (const method) what is it emitting a signal for?
In my app, the const method is to parse data from a pdf file, when it fails, it's helpful to see the logs, and GUI is more powerful to show the logs. If signals are emitted at key points in the method, the signals can be connected to a widgets,then the logs could be shown instantly and nicely.
@jronald
I do understand. But wanting to do logging, from aconst
object, can often lead to such an issue, not just in Qt.It is a shame C++ does not seem to allow methods as
mutable
, like you can for variables, so you can use them fromconst
objects/methods!Did you consider sticking with your
const
signal and suppressing theclazy-const-signal-or-slot
warning? -
@jronald
I do understand. But wanting to do logging, from aconst
object, can often lead to such an issue, not just in Qt.It is a shame C++ does not seem to allow methods as
mutable
, like you can for variables, so you can use them fromconst
objects/methods!Did you consider sticking with your
const
signal and suppressing theclazy-const-signal-or-slot
warning? -
@JonB said in const member function can't emit a signal that is not const:
Did you consider sticking with your const signal and suppressing the clazy-const-signal-or-slot warning?
Not yet, I'm finding a proper way.
-
@jronald
Consider either of the "workarounds" suggested in https://stackoverflow.com/a/5787589/489865.@JonB said in const member function can't emit a signal that is not const:
@jronald
Consider either of the "workarounds" suggested in https://stackoverflow.com/a/5787589/489865.It suggests adding
const
to signal.
In this way no warning when building, only Qt Creator shows a warning.
In another way removingconst
for the methods that emits signals meets Qt's convention.
It should be a personal choice.Thanks