我想在 PyQt 中使用 QrubberBand 和 QRect 在 UI 中通过选取框矩形选择来选择或激活一堆按钮。
如果有人能告诉我如何实现此功能,我将不胜感激?
这是一个简单的演示:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
layout.setMargin(15)
layout.setSpacing(10)
for text in 'One Two Three Four Five'.split():
layout.addWidget(QtGui.QPushButton(text, self))
self.rubberband = QtGui.QRubberBand(
QtGui.QRubberBand.Rectangle, self)
self.setMouseTracking(True)
def mousePressEvent(self, event):
self.origin = event.pos()
self.rubberband.setGeometry(
QtCore.QRect(self.origin, QtCore.QSize()))
self.rubberband.show()
QtGui.QWidget.mousePressEvent(self, event)
def mouseMoveEvent(self, event):
if self.rubberband.isVisible():
self.rubberband.setGeometry(
QtCore.QRect(self.origin, event.pos()).normalized())
QtGui.QWidget.mouseMoveEvent(self, event)
def mouseReleaseEvent(self, event):
if self.rubberband.isVisible():
self.rubberband.hide()
selected = []
rect = self.rubberband.geometry()
for child in self.findChildren(QtGui.QPushButton):
if rect.intersects(child.geometry()):
selected.append(child)
print 'Selection Contains:\n ',
if selected:
print ' '.join(
'Button: %s\n' % child.text() for child in selected)
else:
print ' Nothing\n'
QtGui.QWidget.mouseReleaseEvent(self, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())