Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:04:33+00:00 2026-06-03T16:04:33+00:00

When i drag and drop the button into the QTableWidget, the button disappers from

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:04:35+00:00Added an answer on June 3, 2026 at 4:04 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to perform a simple drag-n-drop operation starting from one button in
It's easy enough to drag and drop bar button items onto a view controller's
I'm experimenting with HTML5's Drag and Drop. I'm trying to drag an html button.
i want to drag and drop my UIButton from one place to another, and
I find drag-and-drop difficult (you know, keeping the mouse button depressed while moving), and
I want to make a drag and drop button. Drag it where you want
i'm trying to drag and drop a button the problem is that when i
I've used delegates when designing win forms in .NET... i.e. drag/drop a button, double
i made a simple form designer in delphi, drag and drop a button on
Is there a good .Net component for drag/drop/resize drawing in 2D with connections (like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.