当在同一个 QMainWindow 中使用 QWebEngineView 时,我遇到了 QPropertyAnimation 的问题。
如果我创建 15 个 QPropertyAnimation(动画 QGraphicsRectItem),同时我使用 QWebEngineView,那么动画会变得非常慢。
我创建了一个演示(自己编译),用最少的有用源代码:https ://media.nperf.com/files/misc/src_sandbox_web_animation.zip
使用一个 QMainWindow 进行的测试:
创建 QWebEngineView 后,如果我杀死它,问题仍然存在。
如果我使用QTimer 循环而不是 QPropertyAnimation,问题是一样的。
使用两个 QMainWindow 进行的测试:
如果我将 QWebEngineView 嵌入到外部窗口(新的 QMainWindow)中,那么动画就会再次变得流畅。这个新的 QMainWindow 与运行 QPropertyAnimations 的 QMainWindow 不同。
但是,这对我来说不是一个可接受的解决方案,但这表明存在 QWebEngineView 和 QPropertyAnimations 之间共享的资源,当且仅当 QWebEngineView 是在同一个 QMainWindow 中创建的。
所以我想知道:
- 我是否正确杀死了 QWebEngineView ?
- 有没有办法将 QWebEngineView 线程与 QMainWindow 分开?
- 或任何其他解决方案...
环境上下文:
这个问题存在两个环境,win10/Qt5.15.2和Mac/Qt5.15.0。
但在 ubuntu20/Qt5.12 和 ubuntu18/Qt5.9.2 中不存在。
可复现的代码示例(演示共享的代码说明):
- QGraphicsRectItem 的 QPropertyAnimation 改变大小:
class AnimationBarItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT
Q_PROPERTY(QRectF rect READ rect WRITE setRect)
...
}
class AnimationBar : public QGraphicsView
{
Q_OBJECT
...
AnimationBarItem *bar = nullptr;
}
void AnimationBar::setPourcent(qreal pourc){
QPropertyAnimation *animation = new QPropertyAnimation(bar, "rect");
animation->setDuration(200);
animation->setStartValue(QRect(0, 0, 0, mH));
animation->setEndValue(QRect(0, 0, ww,mH));
animation->start();
}
- QWebEngineView 创建如下:
mWebEngineView = new QWebEngineView();//can't link to this, for compatibility with my project
mWebEnginePage = new QWebEnginePage(mWebEngineView);
mWebEnginePage->setView(mWebEngineView);
mWebEngineView->setUrl(QUrl("http://www.google.com"));
- 然后使用方法添加 QMainWindow :
mLayoutForBrowse=new QVBoxLayout(ui->ZONE_webview);
mLayoutForBrowse->addWidget(mWebEngineView);
- 或者,在一个新的 QMainWindow 中:
而不是在与前一点相同的 QMainWindow 中,使用:
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
- 杀死 webview :
void MainWindow::killWebView(bool is){
if (mWebEngineView!=nullptr) mWebEngineView->stop();
mWebEngineView->deleteLater();
mLayoutForBrowse->deleteLater();
mWebEnginePage->deleteLater();
mWebEngineView=nullptr;
mLayoutForBrowse=nullptr;
mWebEnginePage=nullptr;
}