Results 1 to 6 of 6

Thread: QTableWidget signal when item dropped

  1. #1
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTableWidget signal when item dropped

    Hi Guys

    Before I go and subclass QTableWidget, does anyone know of a signal or some other way I can be alerted when an item is dropped in a QTableWidget?
    Or do I have to subclass it and reimplement QDropEvent?

    Thanks
    Jeff

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget signal when item dropped

    As far as I know there's no such signal but you could install an event filter on the table widget and handle the drop there instead of using a signal.

  3. #3
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget signal when item dropped

    Just reimplement QWidget::dragEnterEvent( QDragEnterEvent * event ) function.

  4. #4
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableWidget signal when item dropped

    Couldn't get the event filter working - I'm assuming it has something to do with QTableWidget having its own implementation of QEvent and QDropEvent.
    Reimplementing dragEnterEvent was no good to me as I need to know when an item had actually been dropped - a dragEnterEvent fires regardless of whether the item is dropped or not.
    As it is I just subclassed QTableWidget and reImplemented QDropEvent with the following code:

    Qt Code:
    1. void JB_QTableWidget::dropEvent(QDropEvent * event)
    2. {
    3. QTableWidget::dropEvent(event);
    4. emit itemDropped();
    5. event->accept();
    6. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget signal when item dropped

    Hm , what about to emit signal before event is handled by QTableWidget::dropEvent , also don't see a point in "event->accept()". I mean , i think event will be already handled at this point.
    Anyway , i've used to to use QWidget for drag/drop implementation...and any signals' emission seem to work just fine.Here is code snippet :
    Qt Code:
    1. void MyWidget::dragEnterEvent( QDragEnterEvent * event )
    2. {
    3. if( event->provides( ApplicationConstants::supportedMIMEdata) ) event->acceptProposedAction(); // make sure that data being droped can be handled.
    4. }
    5.  
    6. void MyWidget::dropEvent( QDropEvent * event )
    7. {
    8.  
    9. const QMimeData* data = event->mimeData();
    10.  
    11. // ...
    12.  
    13. emit dataIsDroped();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget signal when item dropped

    If the drop signal is the only thing you want and will be used in a signgle place then subclassing is an overkill.

    Just use event filter like I've mentioned earlier:
    Qt Code:
    1. bool MainWindow::eventFilter( QObject* o, QEvent* e )
    2. {
    3. if( o == this->table->viewport() && e->type() == QEvent::Drop )
    4. {
    5. // do what you would do in the slot
    6. qDebug() << "drop!";
    7. }
    8.  
    9. return false;
    10. }
    To copy to clipboard, switch view to plain text mode 
    10 lines of code is all you need.

    In case you don't know how to install even filter here's how:
    Qt Code:
    1. MainWindow::MainWindow( QWidget* p )
    2. :
    3. table( new QTableWidget() )
    4. {
    5. this->table->setColumnCount( 10 );
    6. this->table->setRowCount( 10 );
    7. this->table->setDragEnabled( true );
    8. this->table->setDragDropMode( QAbstractItemView::DragDrop );
    9. this->table->viewport()->installEventFilter( this ); // that's what you need
    10.  
    11. this->setCentralWidget( this->table );
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Item edit in QTableWidget
    By manmohan in forum Newbie
    Replies: 3
    Last Post: 29th April 2011, 17:56
  2. How to select the dropped item in a QTreeWidget?
    By Olivier Berten in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2010, 12:36
  3. Replies: 2
    Last Post: 29th April 2010, 21:44
  4. QTableWidget::item to Double?
    By Afflicted.d2 in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2006, 07:23
  5. Problem of Core Dropped!
    By Stephano in forum General Programming
    Replies: 1
    Last Post: 10th May 2006, 16:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.