QGraphicsItem::paint
has the following signa开发者_开发知识库ture:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
When I create custom QGraphicsItem
s, I have to provide an implementation for this function. The thing is... I never need to use the option
and widget
parameters, but I can't just remove them for obvious reasons. I always see these compiler warnings:
warning: unused parameter ‘widget’
warning: unused parameter ‘option’
Is there a proper to get rid of these warnings? I know I can hide them by mentioning the unused parameters in the function, but this is a very dirty solution and I'd like to know if there are better options.
Either omit the parameter name:
void paint( ..., QWidget* ) {
or use the Q_UNUSED macro:
void paint( ..., QWidget* widget ) {
Q_UNUSED( widget )
...
Don't provide parameter names to the unused parameters. For example (I'm not sure what your "event" refers to), to get rid of warning: unused parameter ‘option’
, change your signature to:
void paint(QPainter *painter, const QStyleOptionGraphicsItem * /* unused */, QWidget *widget)
(The /* unused */
tag is unnecessary as far as the warning goes, but it's useful for the human reader, I find.)
You basically have three proper ways how to silence the unused-parameter compiler warning:
Explicitly cast the unused parameters to void:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
(void)option;
(void)widget;
// ...
}
This pattern is standard conforming and is widely understood by programmers. Compilers like GCC see that the parameter is used, thus don't emit a warning and they optimized those lines away because they are no-ops (i.e. have no effect).
Alternatively, you can use Qt's Q_UNUSED
macro:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
// ...
}
For GCC and others it expands also into the (void)
-cast pattern, but it may also include workarounds for obscure compilers for which it is not enough. For example, with Qt 5.4.1 it is defined as:
#if defined(Q_CC_RVCT)
template <typename T>
inline void qUnused(T &x) { (void)x; }
# define Q_UNUSED(x) qUnused(x);
#else
# define Q_UNUSED(x) (void)x;
#endif
Using Q_UNUSED
is arguably easier to read and must be used for contributions to Qt.
The third option to comment out the parameter names:
void paint(QPainter *painter, const QStyleOptionGraphicsItem * /* option */,
QWidget * /* widget */)
{
// ...
}
This is arguably harder to read. In addition, it perhaps increases temptations to completely delete the parameter names - which may make the function harder to understand. When changing the code to make use of before unused parameters this style also involves more edit operations than the others, of course.
use the Qt macro: Q_UNUSED
to do that
You can omit the parameter name to tell the compiler it is not used:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
You can also set the compile flags in your .pro file. These are the flags for gcc:
QMAKE_CXXFLAGS += -Wno-unused-parameter
QMAKE_CFLAGS += -Wno-unused-parameter
This way you don't have to make changes to your code to suppress this warning.
This may not be for everyone but it's another option.
精彩评论