Qt,Qt5: fixed broken TTrayIcon popup menu after recreating popup menu handle. issue #38849

This commit is contained in:
Željan Rikalo 2021-10-19 14:54:10 +02:00
parent 4aaf8d270c
commit 7f0e0cd0bd
2 changed files with 49 additions and 1 deletions

View File

@ -21,7 +21,7 @@ interface
uses
Classes, types, Controls, ExtCtrls, Graphics, Forms, LCLType, LCLProc, LazUTF8,
qtobjects, qt4, qtproc, qtint;
qtobjects, qt4, qtint;
type
TSysTrayIconPaintData = record
@ -36,11 +36,13 @@ type
TQtSystemTrayIcon = class(TQtObject)
private
FSysTrayHook: QObject_hookH;
FDestroyedMenuHook: QObject_hookH;
FHook: QSystemTrayIcon_hookH;
FSysTrayWidget: QWidgetH;
FCanvas: TCanvas;
function BeginPaintInternal(var APaintData: TSysTrayIconPaintData): hdc;
procedure EndPaintInternal(var APaintData: TSysTrayIconPaintData);
procedure DestroyedMenu; cdecl;
public
FTrayIcon: TCustomTrayIcon;
public
@ -110,6 +112,11 @@ begin
QSystemTrayIcon_hook_destroy(FHook);
FHook := nil;
end;
if Assigned(FDestroyedMenuHook) then
begin
QObject_hook_Destroy(FDestroyedMenuHook);
FDestroyedMenuHook := nil;
end;
inherited DetachEvents;
end;
@ -169,6 +176,13 @@ begin
APaintData.Context := nil;
end;
procedure TQtSystemTrayIcon.DestroyedMenu; cdecl;
begin
setContextMenu(nil);
if not (csDestroying in FTrayIcon.ComponentState) then
FTrayIcon.InternalUpdate;
end;
function TQtSystemTrayIcon.EventFilter(Sender: QObjectH; Event: QEventH): Boolean; cdecl;
var
X, Y: Integer;
@ -244,6 +258,16 @@ end;
procedure TQtSystemTrayIcon.setContextMenu(menu: QMenuH);
begin
if menu <> nil then
begin
if Assigned(FDestroyedMenuHook) then
begin
QObject_hook_destroy(FDestroyedMenuHook);
FDestroyedMenuHook := nil;
end;
FDestroyedMenuHook := QObject_hook_create(menu);
QObject_hook_hook_destroyed(FDestroyedMenuHook, @DestroyedMenu);
end;
QSystemTrayIcon_setContextMenu(QSystemTrayIconH(TheObject), menu);
end;

View File

@ -36,11 +36,13 @@ type
TQtSystemTrayIcon = class(TQtObject)
private
FSysTrayHook: QObject_hookH;
FDestroyedMenuHook: QObject_hookH;
FHook: QSystemTrayIcon_hookH;
FSysTrayWidget: QWidgetH;
FCanvas: TCanvas;
function BeginPaintInternal(var APaintData: TSysTrayIconPaintData): hdc;
procedure EndPaintInternal(var APaintData: TSysTrayIconPaintData);
procedure DestroyedMenu; cdecl;
public
FTrayIcon: TCustomTrayIcon;
public
@ -110,6 +112,11 @@ begin
QSystemTrayIcon_hook_destroy(FHook);
FHook := nil;
end;
if Assigned(FDestroyedMenuHook) then
begin
QObject_hook_Destroy(FDestroyedMenuHook);
FDestroyedMenuHook := nil;
end;
inherited DetachEvents;
end;
@ -168,6 +175,13 @@ begin
APaintData.Context := nil;
end;
procedure TQtSystemTrayIcon.DestroyedMenu; cdecl;
begin
setContextMenu(nil);
if not (csDestroying in FTrayIcon.ComponentState) then
FTrayIcon.InternalUpdate;
end;
function TQtSystemTrayIcon.EventFilter(Sender: QObjectH; Event: QEventH
): Boolean; cdecl;
var
@ -246,6 +260,16 @@ end;
procedure TQtSystemTrayIcon.setContextMenu(menu: QMenuH);
begin
if menu <> nil then
begin
if Assigned(FDestroyedMenuHook) then
begin
QObject_hook_destroy(FDestroyedMenuHook);
FDestroyedMenuHook := nil;
end;
FDestroyedMenuHook := QObject_hook_create(menu);
QObject_hook_hook_destroyed(FDestroyedMenuHook, @DestroyedMenu);
end;
QSystemTrayIcon_setContextMenu(QSystemTrayIconH(TheObject), menu);
end;