PyQt4—QLabel--setBuddy

来源:互联网 发布:游戏编程学校 编辑:程序博客网 时间:2024/04/27 02:33

image 
# -*- coding: UTF-8 –*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class FindCell(QDialog):
    def __init__(self,parent=None):
        super(FindCell,self).__init__(parent)
        self.setWindowTitle('Find Cell')
        lblFind=QLabel('&Find')
        editFind=QLineEdit()
        #lblFind.setBuddy(editFind)
        btnOk=QPushButton('&OK')
        btnCancel=QPushButton('&Cancel')
        mainLayout=QGridLayout(self)
        mainLayout.addWidget(lblFind,0,0)
        mainLayout.addWidget(editFind,0,1,1,2)
        mainLayout.addWidget(btnOk,1,1)
        mainLayout.addWidget(btnCancel,1,2)
app=QApplication(sys.argv)
dlg=FindCell()
dlg.show()
sys.exit(app.exec_())

image 
# -*- coding: UTF-8 –*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class FindCell(QDialog):
    def __init__(self,parent=None):
        super(FindCell,self).__init__(parent)
        self.setWindowTitle('Find Cell')
        lblFind=QLabel('&Find')
        editFind=QLineEdit()
        lblFind.setBuddy(editFind)
        btnOk=QPushButton('&OK')
        btnCancel=QPushButton('&Cancel')
        mainLayout=QGridLayout(self)
        mainLayout.addWidget(lblFind,0,0)
        mainLayout.addWidget(editFind,0,1,1,2)
        mainLayout.addWidget(btnOk,1,1)
        mainLayout.addWidget(btnCancel,1,2)
app=QApplication(sys.argv)
dlg=FindCell()
dlg.show()
sys.exit(app.exec_())

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qlabel.html#setBuddy

QLabel.setBuddy (self, QWidget)

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.

See also buddy(), setText(), QShortcut, and setAlignment().

原创粉丝点击