Important: Please read the Qt Code of Conduct -

[SOLVED] Shift QTableWidget cells after insert at top

  • Hi,

    I am looking for the least CPU intensive way to do a QTableWidget insert at the first index while shifting all other table elements down.

    I currently have a side bar that contains a QTableWidget that contains a list of Custom QWidgets (all of the same class) that populates when specific 'user events' happen in the application. I basically created a custom QTableWidget cell.

    What I am looking to do is insert the newest 'user event' at the top of the list, and shift all of the other items down...Once there is 20 items in the list, I would like to remove the last item in the list, then proceed with inserting the newest 'user event' to the top of the list.

    The issue is that when I insert the widget into the table at row index 0, the other cell widgets to not get shifted.

    Is there another way to shift the widgets in the cells other than looping through every row and moving it one down?

    Here's how I am trying to do this right now, but I am just overwriting the cellWidget in row 0
    // check the row count
    if (ui->table_events->rowCount() >= 20){
    // remove the last row of the table
    ui->table_events->removeCellWidget(ui->table_events->rowCount() - 1, 0);
    ui->table_events->removeRow(ui->table_events->rowCount() - 1);

    // create the cell widget
    SystemEvent *event = new SystemEvent(this);
    // insert the event into the table
    ui->table_events->setCellWidget(0, 0, event);   // pass ownership
    ui->table_events->setRowHeight(0, event->height());
    // show event and do animation
    event->eventShow(title, description, icon, color, 0, 0);


    Edit: There was actually nothing wrong with the existing code...I forgot to create column 0 so that the widgets would actually show... slaps forehead

Log in to reply