Results 1 to 8 of 8

Thread: A more extended explanation for Q_UNUSED macro ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default A more extended explanation for Q_UNUSED macro ?

    I dont understand the benefits of use it.
    Can anybody explain it ?
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A more extended explanation for Q_UNUSED macro ?

    As the docs say:
    Q_UNUSED ( name )

    Indicates to the compiler that the parameter with the specified name is not used in the body of a function. This can be used to suppress compiler warnings while allowing functions to be defined with meaningful parameter names in their signatures.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A more extended explanation for Q_UNUSED macro ?

    I already read the docs.
    I dont understand what means :
    .... with the specified name is not used in the body of a function ...
    Thanks

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A more extended explanation for Q_UNUSED macro ?

    If you have a method which takes two variables and you don't use one of them, then one of them is unused, yes? So you can use Q_UNUSED macro to tell the compiler that you didn't forget about it and you know it's unused, otherwise you could receive warning message.

    Qt Code:
    1. int foo::bar(int a1, int a2, int a3)
    2. {
    3. Q_UNUSED(a3);
    4. int r = a1 + a2;
    5. return r;
    6. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A more extended explanation for Q_UNUSED macro ?

    for example :

    Qt Code:
    1. bool Foo::removeRow(const QModelIndex &index, const QModelIndex &parent)
    2. {
    3. Q_UNUSED(parent);
    4. list.removeAt(index.row());
    5. return true;
    6. }
    To copy to clipboard, switch view to plain text mode 

    The parent parameter is not used inside the body of the function, which could result in a warning. Q_UNUSED here helps avoiding this warning, explicitly telling the compiler you're not using the concerned parameter.

    I didn't really check in the Qt sources, but I guess the Q_UNUSED macro is defined like this :

    Qt Code:
    1. #define Q_UNUSED(arg) (void)arg;
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A more extended explanation for Q_UNUSED macro ?

    Ok, I understand !!!
    ( And I have delete what I had written.)
    Thanks

  7. #7
    Join Date
    May 2012
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A more extended explanation for Q_UNUSED macro ?

    But i can't understand WHY to pass unusued parameter?

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A more extended explanation for Q_UNUSED macro ?

    You might get unused parameters when you implement a slot to match a signal, or implement a virtual function, but do not need everything that is passed in order to complete your processing. For example, you might want to know when the "textChanged(QString)" signal is emitted from a QLineEdit but not care what the text changed to:
    Qt Code:
    1. void handleTextChanged(const QString &text)
    2. {
    3. Q_UNUSED(text);
    4. // make obnoxious noise
    5. }
    To copy to clipboard, switch view to plain text mode 

    You can also suppress the warnings like this:
    Qt Code:
    1. void handleTextChanged(const QString & /*text*/ )
    2. {
    3. // make obnoxious noise
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. A work flow explanation of QPaint ?
    By tonnot in forum Newbie
    Replies: 4
    Last Post: 5th October 2010, 16:26
  2. explanation for Qstring to int
    By qtlinuxnewbie in forum Newbie
    Replies: 1
    Last Post: 11th February 2010, 06:52
  3. Replies: 7
    Last Post: 2nd July 2009, 17:35
  4. Macro
    By comlink21 in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 11:28
  5. Explanation to Image Formats
    By sincnarf in forum Qt Programming
    Replies: 13
    Last Post: 6th July 2007, 17:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.