如何删除或更新每个网格的单元格

2024-06-28 19:46:14 发布

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

现在我希望当用户点击网格中的一个单元格时,它会变得可编辑,他可以编辑单元格值/图像并相应地更新数据。你知道吗

我不想使用内置的编辑,删除和更新按钮的网格。我该怎么做?请引导我。谢谢提前通知你 下面是我的代码:

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_())

Tags: selfforinitdefwidthsettingclassparent
1条回答
网友
1楼 · 发布于 2024-06-28 19:46:14

建立选项最常用的方法是使用上下文菜单,在其中创建一个菜单,在其中添加QAction,并根据相应的选择QAction将返回,这取决于您必须更改图像或将其删除的选择,如下所示(在绘制我使用的项目的图形时采用):

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_())

相关问题 更多 >