1

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.

Could anyone suggest what's the problem?

Please find below the 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() 

1 Answer 1

3

When you drop the button into the table, does the 'print position' work? It should be printing out the coords of the drop position to your shell. I think you need to use those to then insert the button into the table.

Got it working - change your drop event to this:

 position = e.pos() print position row = self.rowAt(position.y()) column = self.columnAt(position.x()) self.setCellWidget(row,column,self.butObject) e.setDropAction(QtCore.Qt.MoveAction) e.accept() 

Cheers

Dave

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it works now, i was looking for a method to find the row and column from the postion object. I looked at the docs but didnt find the methods rowAt() and columnAt(). Thank you, you saved my time :)
Nice! Feel free to hit the green checkmark if this helped. Need to get my points up! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.