Dropping item to bottom of qtablewidget takes moment to update



  • Hi All,
    I drag and drop and item from a table to Qttablewidget. Works fine. When I drop I use an event to record the event to do some calculations on the fly. If I drop on top of an existing item, the code works fine. Row returns it's number and calcs work great. However if I drop to the bottom of the table ( pos -1 ) the calcs don't work. It doesn't see the table updated till after the event handler :s. Is there a way I can make the table update in the handler if it's at the bottom of the table?
    I only found out the update happens after the even from the below test code. Using dropping an item and drawing a number in the second column and forcing process update. I can step through and see the last item is not updated till after the event handler :S can anyone advise. This is the handler, with my debug code so you can see I 'm updating the process event, but the item dropped is not added till after it's left the event. I just add numbers to column 2 as a test to view updates. I presume this is because the dropped item hasn't made 'new' row at the bottom of the column yet? My code is

        if( object == ui->tableWidget->viewport() && event->type() == QEvent::Drop )
        {
    
           QDropEvent *dropevent = dynamic_cast< QDropEvent *>(event);
    
            QPoint position = dropevent->pos();
            int y = position.y();
    
    
            /*NOTE -1 is at end of table*/
            if( ui->tableWidget->rowAt( y ) != -1 )
            {
                QString value = "2";
                ui->tableWidget->setItem( ui->tableWidget->rowAt( y ) , 1, new QTableWidgetItem( value ) );
                calcupdate( );
            }
            else
            {/*normally calc called here this is just to show debug*
                qDebug()<<ui->tableWidget->rowCount();
    qApp->processEvents();
                draw_widget();
    qApp->processEvents();
                QString value = "3";
                ui->tableWidget->setItem( ui->tableWidget->rowCount() , 1, new QTableWidgetItem( value ) );
    qApp->processEvents();
            }
    
    
        }
    

    PS Sorry this is hard to put down in words to show would be easy but describing....



  • try

    ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
    ui->tableWidget->setItem( ui->tableWidget->rowCount()-1 , 1, new QTableWidgetItem( value ) );
    

    instead of

    ui->tableWidget->setItem( ui->tableWidget->rowCount() , 1, new QTableWidgetItem( value ) );
    


  • @VRonin said:

    ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
    ui->tableWidget->setItem( ui->tableWidget->rowCount()-1 , 1, new QTableWidgetItem( value ) );

    Thanks but does'n work it adds new row with number in column 2, then row with item in column 1, empty column 2

    so rather than

    apple 12
    orange 8
    grape 3 <drop

    I get

    apple 12
    orange 8
    3 << nothing column 1
    grape << drop shows up here

    It's got to be with the item being added to the table after the drop event. I think your code adds row, then drop event adds new row but it's called after event handler... Need to say update even now, but have no idea how.



  • are you sure you did not forget the -1 in ui->tableWidget->setItem( ui->tableWidget->rowCount()-1 , 1, new QTableWidgetItem( value ) ); ?+



  • No cut and pasted. The issue ( I think!) is drop event is called, I put in your code snippet. However even through the drop interrupt is called qt has not placed the item on the widget. So I instantly draw new row add number. Then after QT has finished processing drop ( not the drop event I've call which must take priority in processing ) the item is added. So I get 2 rows added.
    I can'r say +1 either because row dosen't exist till after processing.



  • call event->accept() before returning



  • Thanks I'll give that a try. I've got it working but I know it's not idea. Basically when I add to end of table via drop, I set a flag and store the row. This allows the event handler to exit. On next event loop, the drop has been processed. I now check for flag on entering if seen update the second column. AWFUL! But working. I'll try yours as well :)

    bool Reels_Edit_Dialog::eventFilter( QObject* object, QEvent* event )
    {
    
    
        if( drop_event_triggered == true )
        {
            if(  ui->tableWidget->rowCount()  >= drop_event_row_to_update  )
            {
                QString value = "3";
                ui->tableWidget->setItem( drop_event_row_to_update , 1, new QTableWidgetItem( value ) );
                drop_event_triggered = false;
            }
        }
    
    
    
    
        if( object == ui->tableWidget->viewport() && event->type() == QEvent::Drop )
        {
    
    
    
            QDropEvent *dropevent = dynamic_cast< QDropEvent *>(event);
    
            QPoint position = dropevent->pos();
            int y = position.y();
    
    
    
    
            if( ui->checkBox->isChecked() )
            {
    
    
            /*NOTE -1 is at end of table*/
            if( ui->tableWidget->rowAt( y ) != -1 )
            {
                QString value = "2";
                ui->tableWidget->setItem( ui->tableWidget->rowAt( y ) , 1, new QTableWidgetItem( value ) );
            }
            else
            {
    
                drop_event_triggered = true;
                qDebug()<<"rows "<< ui->tableWidget->rowCount( );
                drop_event_row_to_update = ui->tableWidget->rowCount();
    
    
            }
    


  • unfortunately event->accept(); doesn't work. It stop the drop working altogether, when I drop it just vanishes.



  • sorry, my bad, I did not explain properly, I meant in combination with my previous solution

    // Add the row at the bottom
    ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
    // Set the data of the new row
    ui->tableWidget->setItem( ui->tableWidget->rowCount()-1 , 1, new QTableWidgetItem( value ) );
    // Do not let anybody else handle the event
    event->accept();
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.