原理很简单,就是用WebView打开Live2D的页面,固定page所在的位置以及大小,然后将WebView的页面设置为透明,再固定到屏幕右下角即可。
Desktopcute.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_Desktopcute.h"
class QWebEngineView;
class Desktopcute : public QWidget
{
Q_OBJECT
public:
Desktopcute(QWidget *parent = Q_NULLPTR);
Desktopcute(QString HtmlPath);
private:
Ui::DesktopcuteClass ui;
public:
QWebEngineView* WebView;
};
Desktopcute.cpp
#include "Desktopcute.h"
#include <QWebEngineView>
Desktopcute::Desktopcute(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
Desktopcute::Desktopcute(QString HtmlPath)
{
WebView = new QWebEngineView(this);
WebView->resize(800, 300);
WebView->page()->setBackgroundColor(Qt::transparent);
WebView->setAttribute(Qt::WA_TranslucentBackground);
WebView->setStyleSheet("background:transparent");
WebView->page()->setBackgroundColor(Qt::transparent);
WebView->page()->load(QUrl(HtmlPath));
WebView->show();
}
main.cpp
#include "Desktopcute.h"
#include <QtWidgets/QApplication>
#include "QScreen"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Desktopcute w(a.applicationDirPath() + "/Live2D/demo.html");
QScreen *screen = QGuiApplication::primaryScreen();
QRect mm = screen->availableGeometry();
w.setGeometry(mm.width() - 300, mm.height() - 300, 300, 300);
w.setAttribute(Qt::WA_TranslucentBackground);
w.setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
w.show();
return a.exec();
}