问题
I'm working with a group of other programmers on an open source project built using C++ and Qt.
Now, we need a naming convention for widgets (and other variables generally) to use it as a standard in all of our code so that, the code gets better readability and we get better coordination between programmers.
Any advice?
EDIT: Am not talking about naming new classes,
instead I am talking about naming instances of Qt Widgets, let's say I have a text edit for user name, should I name it txtEdtUsrNm?
And in that case, how am I supposed to choose derivations?
回答1:
As long as you are thinking along these lines, I'd start by reading this QtQuarterly article from start-to-finish.
Designing Qt-Style C++ APIs
That said, one thing that we do is to put the "use" of the instance as the first part and the last full word of the class as the last part.
So, your "user name" QTextEdit is
QTextEdit * userNameEdit = new QTextEdit(this);
If there is ambiguity, such as QListView and QTreeView, pick the last unabiguous section.
QListView * userListView;
You can figure out abbreviations how you like (such as "Lbl" for QLabel), but generally, the whole word has worked and has been easy to read.
On the other hand, we are not too strict about this and it might be more important to name the intention of the instance variable without the class name because if, in the future, you want to change the class, you get to change the name which, in the absence of good refactoring tools, is a pain.
Maybe figure out the general widgets you use the most, and pick a naming convention for the most general super-classes and let everything else go.
Example list of things that adhere to the convention:
- layout = All classes that end in "Layout" and inherit QLayout
- button = All classes that end in "Button" and inherit QAbstractButton
The QAbstractClassName classes are a good place to think about what should be in that list.
回答2:
Since Qt is open to third party developers, they made available several documentation regarding the coding style and the API design principles.
These documents are really interesting. Here are a few links :
Wiki : Qt Coding Style
Wiki : Api Design Principles
And this document (PDF) from Jasmin Blanchette, which is worth reading : Little Manual of API Design
回答3:
I would just use the same naming convention that Qt uses. Make it easy for yourself.
Their naming convention is similar to java.
回答4:
The full names quickly become bothersome to read and type, so we're usually using an abbreviation:
QLabel * fooLB;
QPushButton * fooPB;
QTextEdit * fooTE;
QSpinBox * fooSB;
(you get the drift). Yes, there are some ambiguities, but they're usually resolved from context.
回答5:
To be precise,the question is about "naming instances of Qt Widgets", but the answers are about naming variables holding references to instances of widgets.
Qt Widgets derive from QObject which has a name property with getter objectName(). Does Qt give the property a value if the programmer does not? Call it "auto generated" or "default" value.
来源:https://stackoverflow.com/questions/401851/naming-convention-for-qt-widgets