Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dropping item to bottom of qtablewidget takes moment to update
Forum Updated to NodeBB v4.3 + New Features

Dropping item to bottom of qtablewidget takes moment to update

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tony67
    wrote on last edited by tony67
    #1

    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....

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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 ) );
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tony67
        wrote on last edited by tony67
        #3

        @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.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tony67
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              call event->accept() before returning

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tony67
                wrote on last edited by
                #7

                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();
                
                
                        }
                
                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tony67
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    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();
                    

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved