PyQt QTableWidget - 在子类化后恢复双击行为

PyQt QTableWidget - restoring doubleclick behavior after subclassing

我有一个 QTableView,我需要它能够在 QGraphicsScene 中拖动和移动。我通过像这样对 QTableWidget 进行子类化来实现这一点:

def mouseMoveEvent(self, event):
    newPos = self.mapToGlobal(event.pos())
    self.move(newPos)

然而,这似乎覆盖了双击单元格以编辑其文本的能力。有没有简单的方法可以恢复它?

这是一个完整的示例,第二行不可编辑:

import sys
from PyQt4 import QtGui, QtCore

class PicNode(QtGui.QTableWidget):
    def __init__(self):
        super(PicNode, self).__init__()
        picWidth = 480
        picHeight = 240

        self.setFixedSize(picWidth,picHeight+105)
        self.setRowCount(2)
        self.setColumnCount(1)
        self.setColumnWidth(0, picWidth)
        self.setRowHeight(0,picHeight)
        self.setRowHeight(1, 100)
        self.horizontalHeader().hide()
        self.verticalHeader().hide()

        img = QtGui.QPixmap("MV17_Pix BoxR.jpg")
        pic = QtGui.QLabel()
        pic.setPixmap(img.scaled(picWidth,picHeight))
        self.setCellWidget(0,0,pic)
        self.setItem(0,1,QtGui.QTableWidgetItem("some caption data goes here"))

        self.mouseOffset = 0

    def mousePressEvent(self, event):
        # capture the click position on the widget so that when we move it, we offset it by
        # that much
        self.mouseOffset = self.mapFrom(self, event.pos())
        if event.button() != QtCore.Qt.LeftButton:
            event.ignore()
            return

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            newPos = self.mapToGlobal(event.pos())
            self.move(newPos - self.mouseOffset)

class NodeGraph(QtGui.QGraphicsView):
    def __init__(self, parent):
        super(NodeGraph, self).__init__(parent)
        self._zoom = 0
        self._scene = QtGui.QGraphicsScene(self)
        self._node = PicNode()
        #QtGui.QGraphicsProxyWidget(self._scene.addWidget(self._node))
        self._scene.addWidget(self._node)
        self.setScene(self._scene)
        self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
        self.setFrameShape(QtGui.QFrame.NoFrame)
        self.setRenderHint(QtGui.QPainter.Antialiasing)
        self.setDragMode(QtGui.QGraphicsView.RubberBandDrag)

class NodeGraphApp(QtGui.QDialog):
    def __init__(self):
        super(NodeGraphApp, self).__init__()
        self.initUI()

    def initUI(self):
        self.viewer = NodeGraph(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.viewer)

        self.setWindowTitle("Nodegraph test")

def main():
    app = QtGui.QApplication(sys.argv)
    ex = NodeGraphApp()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

问题不在于 mouseMoveEvent,而在于 mousePressEvent。在你自己做之前尝试调用超类方法:

class PicNode(QTableWidget):
    def __init__(self, parent=None):
        super(PicNode, self).__init__(parent)

    def mousePressEvent(self, event):
        super(PicNode,self).mousePressEvent(event)
        # your stuff here