如何保持QPushbutton处于选中状态,然后在按钮网格(pyQt)中查找所选内容

2024-10-03 19:27:37 发布

您现在位置:Python中文网/ 问答频道 /正文

#!!/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,即保持选中状态?在

其次,查询所有按钮,找出当前选中的按钮是哪个?假设它会在点击另一个按钮时释放?所以只有一个按钮被选中。在

任何帮助都会很好。谢谢您。在

干杯


Tags: importselfmainsysguiqt按钮grid
1条回答
网友
1楼 · 发布于 2024-10-03 19:27:37

Firstly, is there any way to keep the QPushButton depressed i.e. remains selected?

是的,您将其设为“可检查”(setCheckable(true)),当“checked”时它是“down”。在

... So there would only ever be a single button selected.

为此,只需将按钮添加到QButtonGroup。它默认为独占的,因此单击其中一个将取消对其他任何对象的强制。在

相关问题 更多 >