Qt 小部件的命名约定

2022-01-18 00:00:00 qt widget coding-style c++

我正在与一群其他程序员合作开发一个使用 C++ 和 Qt 构建的开源项目.

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.

有什么建议吗?

我不是在谈论命名新类,

Am not talking about naming new classes,

我说的是 Qt Widgets 的命名实例,假设我有一个用户名的文本编辑,我应该将其命名为 txtEdtUsrNm 吗?

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?

推荐答案

只要你按照这些思路思考,我就会从头到尾阅读这篇 QtQuarterly 文章.

As long as you are thinking along these lines, I'd start by reading this QtQuarterly article from start-to-finish.

设计 Qt 风格的 C++ API

也就是说,我们要做的一件事是将实例的使用"作为第一部分,将类的最后一个完整单词作为最后部分.

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.

所以,你的用户名"QTextEdit 是

So, your "user name" QTextEdit is

QTextEdit * userNameEdit = new QTextEdit(this);

如果有歧义,比如 QListView 和 QTreeView,选择最后一个不明确的部分.

If there is ambiguity, such as QListView and QTreeView, pick the last unabiguous section.

QListView * userListView;

您可以根据自己的喜好找出缩写词(例如 QLabel 的Lbl"),但一般来说,整个单词都有效且易于阅读.

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.

遵守约定的示例列表:

  • layout = 所有以布局"并继承QLayout
  • button = 以Button"结尾并继承 QAbstractButton 的所有类

QAbstractClassName 类是思考该列表中应该包含什么内容的好地方.

The QAbstractClassName classes are a good place to think about what should be in that list.

相关文章