在实际的工程实践中,使用最多的方法是将手写代码与使用Qt Designer结合起来,如果是多人合作的项目,则使用IDE的时候更多。手写代码几乎无所不能,而仅仅使用Qt Designer是有很大的局限性的。
buddy是专为QLabel设立的,在常见的网络程序上尤为有用,一个QLabel对象有一个对应的buddy(伙伴),当你按下为QLabel对象设置的快捷键时,键盘焦点将立刻转到其对应的buddy上,也就是输入框上面,这就增加了便捷性。
以下是Qt Assistant中的内容,我想不必翻成汉语了,英语更清楚。
Sets this label's buddy to buddy.
When the user presses the shortcut key indicated by this label, the keyboard focus is transferred to the label's buddy widget.
The buddy mechanism is only available for QLabels that contain text in which one character is prefixed with an ampersand, '&'. This character is set as the shortcut key. See the QKeySequence::mnemonic() documentation for details (to display an actual ampersand, use '&&').
In a dialog, you might create two data entry widgets and a label for each, and set up the geometry layout so each label is just to the left of its data entry widget (its "buddy"), for example:
QLineEdit *nameEd = new QLineEdit(this);
QLabel *nameLb = new QLabel("&Name:", this);
nameLb->setBuddy(nameEd);
QLineEdit *phoneEd = new QLineEdit(this);
QLabel *phoneLb = new QLabel("&Phone:", this);
phoneLb->setBuddy(phoneEd);
// (layout setup not shown)
With the code above, the focus jumps to the Name field when the user presses Alt+N, and to the Phone field when the user presses Alt+P.
To unset a previously set buddy, call this function with buddy set to 0.