Now I want that when a user clicks in a cell of the grid, it becomes editable and he can edit the cell values/images and update the data accordingly.
I don't wish to use the in-built edit, delete and update buttons of grid. How can I do that?please guide me.Thank you in advance given bellow is my code:
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 80
HEIGHT = 80
X, Y = 7, 5
class QS(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(QS, self).__init__(QtCore.QRectF(0, 0, X * Setting.WIDTH, Y * Setting.HEIGHT), parent)
def drawBackground(self, painter, rect):
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(Y+1):
painter.drawLine(l)
l.translate(0, Setting.HEIGHT)
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(X+1):
painter.drawLine(l)
l.translate(Setting.WIDTH, 0)
pixmap = QtGui.QPixmap("checkmark.png").scaled(Setting.WIDTH,
Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation)
p = QtCore.QPointF()
for i in range(X):
p = QtCore.QPointF(Setting.WIDTH*i, 0)
for j in range(Y):
painter.drawPixmap(p, pixmap)
p += QtCore.QPointF(0, Setting.HEIGHT)
def mousePressEvent(self,evnt):
# print event
# print(dir(evnt))
print evnt.screenPos().x()
print evnt.screenPos().y()
self.ix = int(evnt.screenPos().x()/Setting.WIDTH)
self.ix = int(evnt.screenPos().y()/Setting.HEIGHT)
print self.ix,self.iy
class QV(QtGui.QGraphicsView):
pass
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QS(self)
view = QV(scene)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
The most common way to establish options is to use a context menu, in it you create a menu where you add the QAction
s, and depending on the selection of the corresponding QAction
will return, depending on the choice you must change the image or delete it as shown below (Take in when to draw the figures I used items):
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 80
HEIGHT = 80
X, Y = 7, 5
class QS(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(QS, self).__init__(QtCore.QRectF(0, 0, X * Setting.WIDTH, Y * Setting.HEIGHT), parent)
pixmap = QtGui.QPixmap("checkmark.png").scaled(Setting.WIDTH, Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation)
for i in range(X):
p = QtCore.QPointF(Setting.WIDTH*i, 0)
for j in range(Y):
item = self.addPixmap(pixmap)
item.setPos(p)
p += QtCore.QPointF(0, Setting.HEIGHT)
def drawBackground(self, painter, rect):
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(Y+1):
painter.drawLine(l)
l.translate(0, Setting.HEIGHT)
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(X+1):
painter.drawLine(l)
l.translate(Setting.WIDTH, 0)
class QV(QtGui.QGraphicsView):
def __init__(self, parent=None):
super(QV, self).__init__(parent)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.on_customContextMenuRequested)
def on_customContextMenuRequested(self, pos):
items = self.items(pos)
if items:
menu = QtGui.QMenu()
change_action = menu.addAction("Change image")
delete_action = menu.addAction("Delete")
action = menu.exec(self.mapToGlobal(pos))
if action is change_action:
filename = QtGui.QFileDialog.getOpenFileName(self,
"Open Image", QtCore.QDir.currentPath() , "Image Files (*.png *.jpg *.bmp)")
if filename:
pixmap = QtGui.QPixmap(filename)
if not pixmap.isNull():
pixmap = pixmap.scaled(Setting.WIDTH, Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)
for item in items:
if isinstance(item, QtGui.QGraphicsPixmapItem):
item.setPixmap(pixmap)
elif action is delete_action:
for it in reversed(items):
self.scene().removeItem(it)
del it
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QS(self)
view = QV(scene)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())