@@ -26,16 +26,27 @@ namespace QWK {
2626 notifyWinIdChange ();
2727 }
2828
29- bool AbstractWindowContext::setHitTestVisible (const QObject *obj, bool visible) {
29+ bool AbstractWindowContext::setHitTestVisible (QObject *obj, bool visible) {
3030 Q_ASSERT (obj);
3131 if (!obj) {
3232 return false ;
3333 }
3434
35+ auto it = m_hitTestVisibleItems.find (obj);
3536 if (visible) {
37+ if (it != m_hitTestVisibleItems.end ()) {
38+ return true ;
39+ }
40+ connect (obj, &QObject::destroyed, this ,
41+ &AbstractWindowContext::_q_hitTestVisibleItemDestroyed);
3642 m_hitTestVisibleItems.insert (obj);
3743 } else {
38- m_hitTestVisibleItems.remove (obj);
44+ if (it == m_hitTestVisibleItems.end ()) {
45+ return false ;
46+ }
47+ disconnect (obj, &QObject::destroyed, this ,
48+ &AbstractWindowContext::_q_hitTestVisibleItemDestroyed);
49+ m_hitTestVisibleItems.erase (it);
3950 }
4051 return true ;
4152 }
@@ -47,27 +58,39 @@ namespace QWK {
4758 return false ;
4859 }
4960
50- if (m_systemButtons[button] == obj) {
51- return false ;
61+ auto org = m_systemButtons[button];
62+ if (org == obj) {
63+ return true ;
64+ }
65+
66+ if (org) {
67+ disconnect (org, &QObject::destroyed, this ,
68+ &AbstractWindowContext::_q_systemButtonDestroyed);
69+ }
70+ if (obj) {
71+ connect (obj, &QObject::destroyed, this ,
72+ &AbstractWindowContext::_q_systemButtonDestroyed);
5273 }
5374 m_systemButtons[button] = obj;
5475 return true ;
5576 }
5677
5778 bool AbstractWindowContext::setTitleBar (QObject *item) {
5879 Q_ASSERT (item);
59- if (m_titleBar == item) {
80+ auto org = m_titleBar;
81+ if (org == item) {
6082 return false ;
6183 }
6284
63- if (m_titleBar ) {
85+ if (org ) {
6486 // Since the title bar is changed, all items inside it should be dereferenced right away
65- for (auto &button : m_systemButtons) {
66- button = nullptr ;
67- }
68- m_hitTestVisibleItems.clear ();
87+ removeSystemButtonsAndHitTestItems ();
88+ disconnect (org, &QObject::destroyed, this ,
89+ &AbstractWindowContext::_q_titleBarDistroyed);
90+ }
91+ if (item) {
92+ connect (item, &QObject::destroyed, this , &AbstractWindowContext::_q_titleBarDistroyed);
6993 }
70-
7194 m_titleBar = item;
7295 return true ;
7396 }
@@ -278,4 +301,38 @@ namespace QWK {
278301 return false ;
279302 }
280303
304+ void AbstractWindowContext::removeSystemButtonsAndHitTestItems () {
305+ for (auto &button : m_systemButtons) {
306+ if (!button) {
307+ continue ;
308+ }
309+ disconnect (button, &QObject::destroyed, this ,
310+ &AbstractWindowContext::_q_systemButtonDestroyed);
311+ button = nullptr ;
312+ }
313+ for (auto &item : m_hitTestVisibleItems) {
314+ disconnect (item, &QObject::destroyed, this ,
315+ &AbstractWindowContext::_q_hitTestVisibleItemDestroyed);
316+ }
317+ m_hitTestVisibleItems.clear ();
318+ }
319+
320+ void AbstractWindowContext::_q_titleBarDistroyed (QObject *obj) {
321+ Q_UNUSED (obj)
322+ removeSystemButtonsAndHitTestItems ();
323+ m_titleBar = nullptr ;
324+ }
325+
326+ void AbstractWindowContext::_q_hitTestVisibleItemDestroyed (QObject *obj) {
327+ m_hitTestVisibleItems.remove (obj);
328+ }
329+
330+ void AbstractWindowContext::_q_systemButtonDestroyed (QObject *obj) {
331+ for (auto &item : m_systemButtons) {
332+ if (item == obj) {
333+ item = nullptr ;
334+ }
335+ }
336+ }
337+
281338}
0 commit comments