添加加载动画,优化启动速度

This commit is contained in:
2024-05-22 18:09:41 +08:00
parent e867d2d251
commit 35ada293e5
5 changed files with 100 additions and 41 deletions
@@ -35,5 +35,6 @@ signals:
void sendErrorToMainwindow(backEndError);
void sendPixmapImageWidget(showImgPack*);
void sendInitFinished(bool);
void sendInitString(QString);//初始化日志
};
#endif
+74 -34
View File
@@ -2,6 +2,10 @@
#include <qpainter.h>
#include <Qlabel>
#include <opencv.hpp>
#include <qmovie.h>
#include <qlayout.h>
#include <qtextedit.h>;
#include <globalStruct.h>
static QPixmap cvMatToQPixmap(const cv::Mat& mat) {
QImage qImg;
if (mat.channels() == 3) {
@@ -16,44 +20,80 @@ static QPixmap cvMatToQPixmap(const cv::Mat& mat) {
class loadingWidget : public QWidget {
public:
QLabel* sign;
QMovie* movie;
QTextEdit* initLog;
QGridLayout* glayout = new QGridLayout(this);
loadingWidget(QWidget* parent = nullptr) : QWidget(parent) {
sign = new QLabel(this);
glayout->setAlignment(Qt::AlignCenter);
}
void setStatus(QWidget* widget) {
screenshot_ = QPixmap::grabWindow(widget->winId());
update(); // 更新widget以显示新的截图
}
protected:
void paintEvent(QPaintEvent* event) override {
if (screenshot_.isNull())
return;
QImage image = screenshot_.toImage();
image = image.convertToFormat(QImage::Format_RGB888);
cv::Mat originalMat = cv::Mat(
image.height(),
image.width(),
CV_8UC3,
image.bits(),
image.bytesPerLine());
cv::Mat blurredMat;
cv::blur(originalMat, blurredMat, cv::Size(15, 15)); // 调整模糊半径以获得不同程度的模糊效果
QPixmap blurredPixmap = cvMatToQPixmap(blurredMat);
QPainter painter(this);
painter.drawPixmap(0, 0, blurredPixmap);
// 在模糊图片上绘制文字
void start(QWidget* widget)
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font("微软雅黑", 100, QFont::Bold);
QFont font2("微软雅黑", 90, QFont::Bold);
painter.setFont(font);
painter.setPen(Qt::black);
painter.drawText(this->rect(), Qt::AlignCenter, "WAIT");
painter.setFont(font2);
painter.setPen(Qt::white);
painter.drawText(this->rect(), Qt::AlignCenter, "WAIT");
sign = new QLabel(this);
sign->setAlignment(Qt::AlignBottom| Qt::AlignHCenter);
movie = new QMovie(":/topBar/icon/loading.gif");
movie->setScaledSize(QSize(100, 100));
movie->start();
sign->setMovie(movie);
initLog = new QTextEdit();
initLog->setAlignment(Qt::AlignCenter);
initLog->setStyleSheet(QString("background-color: transparent;"
"border: 0px solid red;"
"border-radius: 0px;"));
initLog->setReadOnly(true);
QFont font("微软雅黑", 8);
initLog->setFont(font);
//initLog->setPalette(palette);
initLog->setTextColor(Qt::white); // 设置文字颜色为蓝色
initLog->append("测试测试");
glayout->addWidget(sign, 0, 0);
glayout->addWidget(initLog, 1, 0);
glayout->setRowStretch(0, 5);
glayout->setRowStretch(1, 4);
screenshot_ = QPixmap::grabWindow(widget->winId());
update(); // 更新widget以显示新的截图
}
void stop()
{
delete sign;
delete movie;
}
protected:
void paintEvent(QPaintEvent* event) override {
//if (screenshot_.isNull())
// return;
//QImage image = screenshot_.toImage();
//image = image.convertToFormat(QImage::Format_RGB888);
//cv::Mat originalMat = cv::Mat(
// image.height(),
// image.width(),
// CV_8UC3,
// image.bits(),
// image.bytesPerLine());
//cv::Mat blurredMat;
//cv::blur(originalMat, blurredMat, cv::Size(10, 10)); // 调整模糊半径以获得不同程度的模糊效果
//QPixmap blurredPixmap = cvMatToQPixmap(blurredMat);
//QPainter painter(this);
//painter.drawPixmap(0, 0, blurredPixmap);
QWidget::paintEvent(event); // 调用父类的 paintEvent 函数,绘制控件本身
//// 在模糊图片上绘制文字
//QPalette palette;
//palette.setColor(QPalette::WindowText, Qt::white);
//QFont font("微软雅黑", 100, QFont::Bold);
//QFont font2("微软雅黑", 90, QFont::Bold);
//painter.setFont(font);
//painter.setPen(Qt::black);
//painter.drawText(this->rect(), Qt::AlignCenter, "WAIT");
//painter.setFont(font2);
//painter.setPen(Qt::white);
//painter.drawText(this->rect(), Qt::AlignCenter, "WAIT");
}
private:
+1
View File
@@ -16,6 +16,7 @@
void connectSignalAndSlots(MainWindow* front, backEndManager* back)
{
QObject::connect(back, &backEndManager::sendPixmapImageWidget, front, &MainWindow::recvPixmapImageWidget);
QObject::connect(back, &backEndManager::sendInitString, front, &MainWindow::recvInitString);
QObject::connect(back, &backEndManager::sendInitFinished, front, &MainWindow::recvInitFinished);
}
int main(int argc, char* argv[])
+1 -1
View File
@@ -237,7 +237,7 @@ void MainWindow::leftBarInit()
}
pageBtns[index]->pressed();
pageWidgets.setCurrentIndex(index);
qInfo() << "switch Page to Index:" << index;
//qInfo() << "switch Page to Index:" << index;
qDebug() << "switch Page to Index:" << index;
});
}
+23 -6
View File
@@ -59,6 +59,7 @@ private:
QWidget* mainWidget;
QWidget* mainWidget2;
bool backEndInit = false;
bool isMoving = false;
QPoint startPos;
QRect startFrameGeometry;
@@ -91,14 +92,15 @@ public slots:
{
if (input == true)
{
blurEffectAnimation->setStartValue(0);
/*blurEffectAnimation->setStartValue(0);
blurEffectAnimation->setEndValue(20);
blurEffectAnimation->start();
_loadingWidget.setStatus(&stackedWidgets);
stackedWidgets.setCurrentIndex(1);
blurEffectAnimation->setStartValue(20);
blurEffectAnimation->setEndValue(0);
blurEffectAnimation->start();
blurEffectAnimation->start();*/
loading(true);
}
else if(input == false)stackedWidgets.setCurrentIndex(0);
}
@@ -109,14 +111,24 @@ public slots:
blurEffectAnimation->setStartValue(0);
blurEffectAnimation->setEndValue(20);
blurEffectAnimation->start();
_loadingWidget.setStatus(&stackedWidgets);
_loadingWidget.start(&stackedWidgets);
stackedWidgets.setCurrentIndex(1);
blurEffectAnimation->setStartValue(20);
blurEffectAnimation->setStartValue(0);
blurEffectAnimation->setEndValue(0);
blurEffectAnimation->start();
}
else if (input == false)stackedWidgets.setCurrentIndex(2);
else if (input == false)
{
blurEffectAnimation->setStartValue(0);
blurEffectAnimation->setEndValue(0);
blurEffectAnimation->start();
_loadingWidget.stop();
stackedWidgets.setCurrentIndex(2);
blurEffectAnimation->setStartValue(20);
blurEffectAnimation->setEndValue(0);
blurEffectAnimation->start();
}
}
void recvPixmapImageWidget(showImgPack* pack)
@@ -125,6 +137,11 @@ public slots:
delete pack;
}
void recvInitFinished(bool input)
{
backEndInit = true;
//loading(false);
}
void recvInitString(QString)
{
}