QTableView动画行隐藏

时间:2017-07-23 11:59:49

标签: c++ qt qtableview

hideRow()使行立即消失,我想要平滑过渡,一些淡入淡出效果。

这可能吗?我在文档中找不到任何参考。

1 个答案:

答案 0 :(得分:1)

您可以使用QTableView::setRowHeightQTimer

/*
 * Get QTableView pointer and index of the row to hide from
 * somewhere.
 */
QTableView *tv = ...;
int row = ...;

/*
 * Create a timer.  It will be deleted by our lambda when no
 * longer needed so no memory leak.
 */
auto *timer = new QTimer;

/*
 * Connect the timer's timeout signal to a lambda that will
 * do the work.
 */
connect(timer, &QTimer::timeout,
        [=]()
        {
          int height = tv->rowHeight(row);
          if (height == 0) {

            /*
             * If the row height is already zero then hide the
             * row and delete the timer.
             */
            tv->setRowHidden(row, true);
            timer->deleteLater();
          } else {

            /*
             * Otherwise, decrease the height of the row by a
             * suitable amount -- 1 pixel in this case.
             */
            tv->setRowHeight(row, height - 1);
          }
        });

/*
 * Start the timer running at 10Hz.
 */
timer->start(100);

请注意,上述代码未经测试。此外,您可能希望将其视为视图类或其他任何内容的成员变量,而不是创建“空灵”QTimer