PyQt QTableWidget - 在子类化

时间:2017-03-29 01:00:50

标签: python pyqt qtablewidget 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()

1 个答案:

答案 0 :(得分:0)

问题不在于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