Forum > QT
How to create menu at runtime?
(1/1)
zamtmn:
It is a bug or I doing something wrong? This code works in gtk2 and win, but in qt causes an error:
--- Code: ---procedure TForm1._onCreateForm(Sender: TObject);
var
MyItem:TMenuItem;
MyMenu:TMainMenu;
begin
MyMenu:=TMainMenu.Create(application);
MyItem:=TMenuItem.Create(MyMenu);
MyItem.Caption:='fail';
MyMenu.items.Add(MyItem);
self.Menu:=MyMenu;
end;
--- End code ---
Lazarus&FPC from trunk
taazz:
change
--- Code: --- MyMenu:=TMainMenu.Create(application);
--- End code ---
to
--- Code: --- MyMenu:=TMainMenu.Create(Self);
--- End code ---
zamtmn:
Thank you, it works. But it should work with Application
zeljko:
--- Quote from: zamtmn on June 13, 2013, 06:44:14 am ---Thank you, it works. But it should work with Application
--- End quote ---
No it cannot work like that since WS.CreateHandle() expects TCustomForm as owner.
class function TQtWSMenu.CreateHandle(const AMenu: TMenu): HMENU;
var
MenuBar: TQtMenuBar;
Menu: TQtMenu;
begin
{ If the menu is a main menu, there is no need to create a handle for it.
It's already created on the window }
if (AMenu is TMainMenu) and (AMenu.Owner is TCustomForm) then <------------ See this
begin
MenuBar := TQtMainWindow(TCustomForm(AMenu.Owner).Handle).MenuBar; <------------- and then this
Result := HMENU(MenuBar);
end
else if (AMenu is TPopUpMenu) then
begin
Menu := TQtMenu.Create(AMenu.Items);
Menu.AttachEvents;
Result := HMENU(Menu);
end;
{$ifdef VerboseQt}
Write('[TQtWSMenu.CreateHandle] ');
if (AMenu is TMainMenu) then Write('IsMainMenu ');
WriteLn(' Handle: ', dbghex(Result), ' Name: ', AMenu.Name);
{$endif}
end;
While creating menu handle parent is still unassigned, so owner is only solution.
Maybe I can add check for Application as owner, but in that case that menu should be treated only as TApplication.MainForm child.
zamtmn:
Thans, if it's not a bug - I will not use Application
Navigation
[0] Message Index