Results 1 to 5 of 5

Thread: Qt specific questions regaring coding style

  1. #1
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt specific questions regaring coding style

    I like to follow a certain coding style for my classes, following the following template :

    Qt Code:
    1. class SomeClass : public SomeBaseClass
    2. {
    3.  
    4. public :
    5. void somePublicFunction();
    6.  
    7. int someVariable() const;
    8. void setSomeVariable(const int& value);
    9.  
    10. float anotherVariable() const;
    11. void setAnotherVariable(const float& value);
    12.  
    13. ...
    14.  
    15. private:
    16. void somePrivateFunction();
    17. ...
    18.  
    19. int someVariable_;
    20. float anotherVariable_;
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    However, when I am working on a Qt project with for example a large QMainWindow derived class, I tend to break this style. There are so many private QAction's, QMenu's, QToolbar's and other Qt based objects that I feel they make the code less readable if I append an underscore.

    Do any of you guys tend to do something similar in specific cases, or do you follow your coding style strictly on every occasion ?

  2. #2
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt specific questions regaring coding style

    If you namespace your private data with a struct, you will avoid the name clashes.

    Qt Code:
    1. class A {
    2. public:
    3. int a() const { return data.a; }
    4. int b() const { return data.b; }
    5. int c() const { return data.c; }
    6.  
    7. private:
    8. struct {
    9. int a, b, c;
    10. } data;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Because the struct itself is inlined, the compiler will optimize away the dereferencing.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt specific questions regaring coding style

    I myself append an underscore to my data members. However, I'm starting to lean towards the "m_" prefix. If not for the inevitable clash between getters (foo()) and members (foo), I would forego special naming conventions completely.

  4. #4
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt specific questions regaring coding style

    I personally just prefix with underscore.

    The issue with it is you break convention when using VS integration as the Ui object which sets up your document doesn't have the underscore. Maybe you can force it, dunno.
    I just personally accept that all Qt 'designed' object won't have the underscore and don't worry about it.

    You could consider that itself a convention...anything you manually define in code has the underscore.

  5. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt specific questions regaring coding style

    In fact, that is the way I thought also to approach it. Thank you all for the input.

Similar Threads

  1. Coding style about pointer
    By vql in forum General Programming
    Replies: 4
    Last Post: 5th February 2007, 09:07
  2. What is your coding style ??
    By guestgulkan in forum General Discussion
    Replies: 9
    Last Post: 29th May 2006, 10:22

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.