0

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()
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
mechapman
  • 25
  • 6

1 Answers1

0

Extend your QWidget derived class to override mousePressEvent and then according to the actual mouse position find the tile and store the part of the pixmap that you want to store. Just add the following method to your class and fill in the specific code for your pixmap cutting out and storage.

def mousePressEvent(event):
  """
    User has clicked inside the widget
  """
  # get mouse position
  x = event.x()
  y = event.y()
  # find coordinates of grid rectangle in your grid
  # copy and store this grid rectangle

You could even display a rectangular rubber band that jumps from rectangle to rectangle if you want that. For this override mouseMoveEvent.

def mouseMoveEvent(event):
  """
    Mouse is moved inside the widget
  """
  # get mouse position
  x = event.x()
  y = event.y()
  # find coordinates of grid rectangle in your grid
  # move rectangular rubber band to this grid rectangle
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • Also see [Python - PyQT4 how to detect the mouse click position anywhere in the window?](http://stackoverflow.com/questions/19825650/python-pyqt4-how-to-detect-the-mouse-click-position-anywhere-in-the-window) – NoDataDumpNoContribution Jul 01 '14 at 09:30
  • Thanks for the answer! I am new to Qt and I quite don't understand how to find the coordinates of the rectangle based on the actual mouse position. Any hints are much appreciated. Thanks in advance. – iamkk Sep 06 '21 at 09:32