#!!/usr/bin/python
# Qt modules
from PyQt4 import QtCore, QtGui
import sys
class gui(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Grid of buttons')
self.resize(450, 300)
#------------------------------------------------------------------------------
# WIDGETS
#------------------------------------------------------------------------------
self.grid = QtGui.QGridLayout()
btnNum = 1
columnCount = 0
rowCount = 0
while btnNum != 26:
button = QtGui.QPushButton(str(btnNum))
self.grid.addWidget(button, rowCount, columnCount)
btnNum += 1
if columnCount == 4:
columnCount = 0
rowCount += 1
else:
columnCount += 1
#------------------------------------------------------------------------------
# LAYOUT
#------------------------------------------------------------------------------
self.idChannelGroup = QtGui.QGroupBox("My Button Layout")
self.idChannelGroup.setLayout(self.grid)
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.idChannelGroup)
self.setLayout(self.mainLayout)
def main():
app = QtGui.QApplication(sys.argv)
ex = gui()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
大家好。。。关于QPushButton的使用,我有两个问题。我对Qt很陌生,所以如果这些看起来有点愚蠢,我会提前道歉。。。在
首先,有没有办法保持按下QPushButton,即保持选中状态?在
其次,查询所有按钮,找出当前选中的按钮是哪个?假设它会在点击另一个按钮时释放?所以只有一个按钮被选中。在
任何帮助都会很好。谢谢您。在
干杯
是的,您将其设为“可检查”(
setCheckable(true)
),当“checked”时它是“down”。在为此,只需将按钮添加到
QButtonGroup
。它默认为独占的,因此单击其中一个将取消对其他任何对象的强制。在相关问题 更多 >
编程相关推荐