Note that there are some explanatory texts on larger screens.

plurals
  1. POQTableWidget - Drag and Drop a Button into a cell in the QTableWidget
    text
    copied!<p>When i drag and drop the button into the QTableWidget, the button disappers from the old position and nothing is displayed in the cell where i drop the button.</p> <p>Could anyone suggest what's the problem?</p> <p>Please find below the code</p> <pre><code>import sys from PyQt4 import QtGui from PyQt4 import QtCore class Button(QtGui.QPushButton): def __init__(self, title, parent): super(Button, self).__init__(title, parent) def mouseMoveEvent(self, e): if e.buttons() != QtCore.Qt.RightButton: return mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.start(QtCore.Qt.MoveAction) def mousePressEvent(self, e): if e.button() == QtCore.Qt.LeftButton: print 'Left Button Pressed' class MyTable(QtGui.QTableWidget): def __init__(self, rows, columns, butObject, parent): super(MyTable, self).__init__(rows, columns, parent) self.setAcceptDrops(True) self.butObject = butObject def dragEnterEvent(self, e): e.accept() def dropEvent(self, e): position = e.pos() print position self.butObject.move(position) e.setDropAction(QtCore.Qt.MoveAction) e.accept() def dragMoveEvent(self, e): e.accept() class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setAcceptDrops(True) self.button = Button('Button', self) self.button.move(50, 200) self.table = MyTable(2, 2, self.button, self) self.table.setAcceptDrops(True) self.table.setDragEnabled(True) self.setWindowTitle('Click or Move') #self.setGeometry(300, 300, 280, 150) def main(): app = QtGui.QApplication(sys.argv) ex = Example() ex.show() app.exec_() if __name__ == '__main__': main() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload