当界面上点击一个按钮,实现截图功能之后,需要反馈给用户一个截图成功的提示。模仿Iphone截图显示。话不多说直接上代码。
头文件
class QPropertyAnimation;
class BlinkFrame : public QFrame
{
Q_OBJECT
public:
explicit BlinkFrame(QWidget *parent = 0);
~BlinkFrame();
void setSnapshotPixmap(QPixmap &pixmap);
void show();
void setInitRect(QRect rect);
private:
QLabel* mBGLabel;
QRect mRect;
QPixmap mFitpixmap;
QPropertyAnimation* mShowAnimal{ nullptr };
};
源文件
BlinkFrame::BlinkFrame(QWidget *parent) :
QFrame(parent)
{
setWindowFlags(Qt::SubWindow | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground, true);
//setWindowOpacity(0.8);
mBGLabel = new QLabel(this);
mBGLabel->setStyleSheet("background-color:white");
mShowAnimal = new QPropertyAnimation(mBGLabel, "geometry");
}
BlinkFrame::~BlinkFrame()
{
}
void BlinkFrame::setSnapshotPixmap(QPixmap &pixmap)
{
mFitpixmap = pixmap;
}
void BlinkFrame::show()
{
mShowAnimal->setDuration(400);
mShowAnimal->setStartValue(mRect);
mShowAnimal->setEndValue(QRect(mRect.x(), mRect.y() + mRect.height()*0.7, mRect.width()*0.3, mRect.height()*0.3));
connect(mShowAnimal, &QPropertyAnimation::finished, this, [=](){
mFitpixmap = mFitpixmap.scaled(mBGLabel->width(), mBGLabel->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
mBGLabel->setPixmap(mFitpixmap);
mBGLabel->setAlignment(Qt::AlignCenter);
QTimer::singleShot(900, [=](){
hide();
this->deleteLater();
});
});
QDesktopWidget* desktop = QApplication::desktop();
int count = desktop->screenCount();
int tempWidth = 0;
int tempHeight = 0;
for (int i = 0; i < count; i++)
{
tempWidth += desktop->screenGeometry(i).width();
}
if (count > 0)
{
tempHeight = desktop->screenGeometry(0).height();
}
this->setGeometry(0, 0, tempWidth, tempHeight);
QFrame::show();
//QFrame::showFullScreen();
mBGLabel->resize(mRect.size());
mBGLabel->move(mRect.x(), mRect.y());
mShowAnimal->start();
}
void BlinkFrame::setInitRect(QRect rect)
{
mRect = rect;
}
应用代码:
BlinkFrame *blinkLabel = new BlinkFrame;
blinkLabel->setInitRect(m_previewRect);
blinkLabel->setSnapshotPixmap(QPixmap("./ScreenSnap.png"));
blinkLabel->show();