Do not leak connections

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2021-03-12 09:01:49 +08:00
parent c19a4f7f74
commit be2a2a48a2
3 changed files with 6 additions and 1 deletions

View File

@ -56,6 +56,7 @@ void QtAcrylicEffectHelper::install(const QWindow *window)
connect(m_window, &QWindow::xChanged, this, &QtAcrylicEffectHelper::needsRepaint); connect(m_window, &QWindow::xChanged, this, &QtAcrylicEffectHelper::needsRepaint);
connect(m_window, &QWindow::yChanged, this, &QtAcrylicEffectHelper::needsRepaint); connect(m_window, &QWindow::yChanged, this, &QtAcrylicEffectHelper::needsRepaint);
connect(m_window, &QWindow::activeChanged, this, &QtAcrylicEffectHelper::needsRepaint); connect(m_window, &QWindow::activeChanged, this, &QtAcrylicEffectHelper::needsRepaint);
// What's the difference between "visibility" and "window state"?
//connect(m_window, &QWindow::visibilityChanged, this, &QtAcrylicEffectHelper::needsRepaint); //connect(m_window, &QWindow::visibilityChanged, this, &QtAcrylicEffectHelper::needsRepaint);
connect(m_window, &QWindow::windowStateChanged, this, &QtAcrylicEffectHelper::needsRepaint); connect(m_window, &QWindow::windowStateChanged, this, &QtAcrylicEffectHelper::needsRepaint);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS

View File

@ -31,11 +31,14 @@
QtAcrylicItem::QtAcrylicItem(QQuickItem *parent) : QQuickPaintedItem(parent) QtAcrylicItem::QtAcrylicItem(QQuickItem *parent) : QQuickPaintedItem(parent)
{ {
connect(this, &QtAcrylicItem::windowChanged, this, [this](QQuickWindow *win){ connect(this, &QtAcrylicItem::windowChanged, this, [this](QQuickWindow *win){
if (m_repaintConnection) {
disconnect(m_repaintConnection);
}
m_acrylicHelper.uninstall(); m_acrylicHelper.uninstall();
if (win) { if (win) {
m_acrylicHelper.install(win); m_acrylicHelper.install(win);
m_acrylicHelper.updateAcrylicBrush(); m_acrylicHelper.updateAcrylicBrush();
connect(&m_acrylicHelper, &QtAcrylicEffectHelper::needsRepaint, this, [this](){ m_repaintConnection = connect(&m_acrylicHelper, &QtAcrylicEffectHelper::needsRepaint, this, [this](){
update(); update();
}); });
} }

View File

@ -77,4 +77,5 @@ Q_SIGNALS:
private: private:
QtAcrylicEffectHelper m_acrylicHelper; QtAcrylicEffectHelper m_acrylicHelper;
bool m_frameVisible = true; bool m_frameVisible = true;
QMetaObject::Connection m_repaintConnection = {};
}; };