Important: Please read the Qt Code of Conduct -

Animate QTableWidget

  • How can I animate the deletion/insertion of a row in a QTableWidget?

  • Lifetime Qt Champion

    In what way animate?
    The items are tightly controlled by the QTableWidget so we cant move them
    around or anything like that.

  • Well I have created rowInsert() and rowDelete() functions and i would like the deleted/inserted rows to be animated when they appear/disappear. I would like animation like for example one on their height property (from 0->100 when it appears, from
    100->0 when it disappears).
    Edit: I found this video as an example of how I want it to appear.

  • Lifetime Qt Champion

    You can do like this

    class RowAnimator : public QObject
        QRect mRect;
        QPropertyAnimation *animation;
        QTableWidget *table = nullptr;
        Q_PROPERTY(QRect ItemRect READ ItemRectRead WRITE ItemRectWrite)
        QRect ItemRectRead() const
            return mRect;
        void ItemRectWrite(const QRect &rect)
            mRect = rect;
        RowAnimator(QObject *parent, QTableWidget *tableTarget) : QObject (parent), table(tableTarget)
            animation = new QPropertyAnimation(this, "ItemRect");
        virtual ~RowAnimator() {}
    public slots:
        void AnimateRow(int row, int start, int end)
            animation->setStartValue(QRect(0, 0, 0, start));
            animation->setEndValue(QRect(0, 0, 0, end));
            connect(animation, &QPropertyAnimation::valueChanged, [ = ](const QVariant & value) {
                QRect rect = value.toRect();
                table->setRowHeight(row, rect.height());

    and call Ra->AnimateRow(4,start height ,end height );

    alt text

    you might want to add a signal for animation finished so you know when its ok to
    actually, remove the row.
    Also if you tweak it a bit and use an easing curve it should look pretty ok.

  • Can you please help me with what coordinates you put in startHeight and endHeight, because no matter what coordinate i put in the resize stops halfway. The animation itself runs until the value of rectangle goes endHeight, but no resizing happens. I think it happens because table.setRowHeight fails for some reason but I don't understand why.

  • Lifetime Qt Champion

    I tested with both small and large values and seems to work.

      connect(ui->pbGo, &QPushButton::released, this, [Ra](){
        connect(ui->pbCome, &QPushButton::released, this, [Ra](){

    what values did you use ?

  • Hi
    I used

    int begin = table.rowHeight(table.currentRow());
    int end = 0;
    int row = table.currentRow();

    as values for animation begin and end.
    I have this piece of code

    connect(anim, &QPropertyAnimation::valueChanged, [&table, row]
    (const QVariant & value) {
       	QRect rect = value.toRect();
       	qDebug() << rect<<"\t";
       	table.setRowHeight(row, rect.height());
       	qDebug() << table.rowHeight(table.currentRow())<<endl;

    which produces this debug info:

    QRect(0,0 0x29)
    QRect(0,0 0x28)
    QRect(0,0 0x27)
    QRect(0,0 0x26)
    QRect(0,0 0x25)
    QRect(0,0 0x24)
    QRect(0,0 0x23)
    QRect(0,0 0x22)
    QRect(0,0 0x21)

    This is what makes me think that it might be a problem with QTableWidget::setRowHeight.

  • Lifetime Qt Champion

    Hmm so it only runs to 23 as minimum row height.
    What if you directly set it to say 10?

  • I have tried setting it directly even to 0 and it works. Maybe there's a limit to number of resizes?

  • Lifetime Qt Champion

    It seems to have a minimum size.
    if i do

      qDebug() << ui->tableWidget->rowHeight(4);

    it says 23.

    ah, its linked to
    alt text

  • @mrjj said in Animate QTableWidget:


    Yes this was it thank you very much, its smooth now!

  • Yes,agreed....Lots of confusions in cordinates of startHeight and endHeight. It just stucks in mysystem after going halfway.Saw couple of codes but all in vain

Log in to reply