views:

147

answers:

0

I am designing a GUI interface in QT Designer. Part of this interface involves two custom widgets stacked on top of each other via a QStackedLayout. When certain actions are performed elsewhere in the GUI, I want the top stacked widget to slide upwards, revealing the widget beneath it and covering whatever happens to be above it.

I am currently accomplishing this with the following code block:

QWidget* k = this->ui->keyboard; // widget to be moved
QPoint endingPoint = k->pos(); // current position of above widget

if (ui->leSearch->pos().y() < k->pos().y()) {
    endingPoint.ry() -= k->height(); // slide pos up; cover area above
    ui->stackedLayout->setCurrentIndex(1);
} else {
    endingPoint.ry() += k->height(); // slide back down to original pos
    ui->stackedLayout->setCurrentIndex(0);
}

QPropertyAnimation *animation = new QPropertyAnimation(k, "pos");
animation->setDuration(750);
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->setEndValue(endingPoint);

animation->start();
k->raise(); // bring to front of GUI

This works as expected, with the following caveat: switching the keyboard focus away from the current application/window will (almost) always cause the animated widget to snap back down into the layout.

Everything works fine if my custom Keyboard widget is not encased in its own layout--just the overall parent layout. (Though when I do that, I can't stack my widgets on top of each other.) But when I place my custom widgets into a QLayout descendant of their very own, that Keyboard keeps sneaking back to its original position.

What's going on here? Is there a better, more sane method of accomplishing my end goal?