基本思想:假設A為主窗口,B為子窗口。A打開或關閉時,先對A窗口進行截圖,然后將圖片部滿整個B窗口的,在paintEvent里面進行動態縮放或放大畫圖。最后使用動畫,將B窗口以動畫的形式打開或關閉,動畫播放完畢后,B發送一個信號給A,B關閉,A顯示出來。
核心代碼發下:
在A窗口里:
QPixmap pixmap;
CTestDialog dlg(this);
dlg.SetPixmap(pixmap.grabWidget(this));
hide();
dlg.exec();
B窗口:
CTestDialog::CTestDialog(QWidget *pParent) : QDialog(pParent)
{
ui.setupUi(this);
setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
}
CTestDialog::~CTestDialog()
{
}
void CTestDialog::SetPixmap(const QPixmap &pixmap)
{
m_backPixmap = pixmap;
m_image = pixmap.toImage();
update();
QDesktopWidget *desktopWidget = QApplication::desktop();
QRect screenRect = desktopWidget->screenGeometry();
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(2200);
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->setStartValue(QRect((screenRect.width() - 50) / 2, (screenRect.height() - 50) / 2, 50, 50));
animation->setEndValue(QRect((screenRect.width() - 500) / 2, (screenRect.height() - 400) / 2, 500, 400));
animation->start();
}
void CTestDialog::paintEvent(QPaintEvent *p)
{
QPalette pal(palette());
pal.setBrush(QPalette::Window, QBrush(m_image.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
setPalette(pal);
}