PDA

View Full Version : QTableWidget signal when item dropped



Jeffb
12th February 2012, 06:56
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

Spitfire
14th February 2012, 14:52
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.

AlexSudnik
14th February 2012, 16:52
Just reimplement QWidget::dragEnterEvent( QDragEnterEvent * event ) function.

Jeffb
17th February 2012, 00:39
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:


void JB_QTableWidget::dropEvent(QDropEvent * event)
{
QTableWidget::dropEvent(event);
emit itemDropped();
event->accept();
}

AlexSudnik
17th February 2012, 08:49
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 :


void MyWidget::dragEnterEvent( QDragEnterEvent * event )
{
if( event->provides( ApplicationConstants::supportedMIMEdata) ) event->acceptProposedAction(); // make sure that data being droped can be handled.
}

void MyWidget::dropEvent( QDropEvent * event )
{

const QMimeData* data = event->mimeData();

// ...

emit dataIsDroped();

}

Spitfire
17th February 2012, 10:36
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:

bool MainWindow::eventFilter( QObject* o, QEvent* e )
{
if( o == this->table->viewport() && e->type() == QEvent::Drop )
{
// do what you would do in the slot
qDebug() << "drop!";
}

return false;
}10 lines of code is all you need.

In case you don't know how to install even filter here's how:
MainWindow::MainWindow( QWidget* p )
:
QMainWindow( p ),
table( new QTableWidget() )
{
this->table->setColumnCount( 10 );
this->table->setRowCount( 10 );
this->table->setDragEnabled( true );
this->table->setDragDropMode( QAbstractItemView::DragDrop );
this->table->viewport()->installEventFilter( this ); // that's what you need

this->setCentralWidget( this->table );
}