I have drawn a grid on a pixmap by painting evenly spaced horizontal and vertical lines, and I am trying to make each of the rectangular grid pieces selectable.
In other words, if a user clicks on a certain rectangle in the grid, then it would be stored as a separate pixmap. I have tried using the QRubberBand
.
But I can't figure out how to restrict the selection to the specific piece that was selected. Is there a way to do this using PyQt?
Here is my code for drawing the grid onto the pixmap:
class imageSelector(QtGui.QWidget):
def __init__(self):
super(imageSelector,self).__init__()
self.initIS()
def initIS(self):
self.pixmap = self.createPixmap()
painter = QtGui.QPainter(self.pixmap)
pen = QtGui.QPen(QtCore.Qt.white, 0, QtCore.Qt.SolidLine)
painter.setPen(pen)
width = self.pixmap.width()
height = self.pixmap.height()
numLines = 6
numHorizontal = width//numLines
numVertical = height//numLines
painter.drawRect(0,0,height,width)
for x in range(numLines):
newH = x * numHorizontal
newV = x * numVertical
painter.drawLine(0+newH,0,0+newH,width)
painter.drawLine(0,0+newV,height,0+newV)
label = QtGui.QLabel()
label.setPixmap(self.pixmap)
label.resize(label.sizeHint())
hbox = QtGui.QHBoxLayout()
hbox.addWidget(label)
self.setLayout(hbox)
self.show()
def createPixmap(self):
pixmap = QtGui.QPixmap("CT1.png").scaledToHeight(500)
return pixmap
def main():
app = QtGui.QApplication(sys.argv)
Im = imageSelector()
sys.exit(app.exec_())
if __name__== '__main__':
main()