Recent

Author Topic: Qt does not position forms  (Read 745 times)

Alexx2000

  • New Member
  • *
  • Posts: 28
Re: Qt does not position forms
« Reply #15 on: May 12, 2026, 08:06:23 pm »
Add this unit to uses before Interfaces unit:

Code: Pascal  [Select][+][-]
  1. unit uQtWorkaround;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. implementation
  8.  
  9. uses
  10.   InitC, BaseUnix;
  11.  
  12. function setenv(const name, value: pchar; overwrite: cint): cint; cdecl; external clib;
  13.  
  14. initialization
  15.   setenv('QT_QPA_PLATFORM', 'xcb', 1);
  16.  
  17. end.

Fred vS

  • Hero Member
  • *****
  • Posts: 3945
    • StrumPract is the musicians best friend
Re: Qt does not position forms
« Reply #16 on: May 12, 2026, 08:07:57 pm »
As always when using AI things don't work, in fact lazarus doesn't recognize the fpGetEnv() and fpSetEnv() functions


Gemini propose this:

Code: Pascal  [Select][+][-]
  1. program YourAppName;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF LINUX}
  7.   Unix, // Required for fpGetEnv and fpSetEnv
  8.   {$ENDIF}
  9.   Interfaces, // Initializes the Lazarus widgetset
  10.   Forms,
  11.   Unit1;
  12.  
  13. {$IFDEF LINUX}
  14. procedure CheckAndForceXCB;
  15. var
  16.   SessionType: String;
  17. begin
  18.   // Retrieve the current desktop session type
  19.   SessionType := LowerCase(fpGetEnv('XDG_SESSION_TYPE'));
  20.  
  21.   // If running on Wayland, force Qt to fallback to XCB (via XWayland)
  22.   if SessionType = 'wayland' then
  23.   begin
  24.     fpSetEnv('QT_QPA_PLATFORM', 'xcb', 1);
  25.   end;
  26. end;
  27. {$ENDIF}
  28.  
  29. begin
  30.   {$IFDEF LINUX}
  31.   // This must execute before the Interfaces unit initializes Qt
  32.   CheckAndForceXCB;
  33.   {$ENDIF}
  34.  
  35.   Application.Initialize;
  36.   Application.CreateForm(TForm1, Form1);
  37.   Application.Run;
  38. end.
  39.  

Indeed, it should write:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF LINUX}
  3.   BaseUnix, // Required for fpGetEnv and fpSetEnv
  4.   {$ENDIF}

Bad gemini.  >:(
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

systemgvp

  • New Member
  • *
  • Posts: 46
Re: Qt does not position forms
« Reply #17 on: May 12, 2026, 08:31:50 pm »
 >:( >:( >:(

Code: Pascal  [Select][+][-]
  1. {$IFDEF LINUX}
  2.   SysUtils, InitC, BaseUnix, // Required for fpGetEnv and fpSetEnv
  3.   {$ENDIF}
  4.  
  5. {$IFDEF LINUX}
  6. //function setenv(name, value: PChar; overwrite: Integer): Integer; cdecl; external 'libc.so.6';
  7. function setenv(const name, value: pchar; overwrite: cint): cint; cdecl; external clib;
  8.  
  9. var SessionType: String;
  10. {$ENDIF}
  11.  
  12. {$IFDEF LINUX}
  13.   //per risolvere il problema dei QT6 su wayland
  14.   SessionType := LowerCase(GetEnvironmentVariable('XDG_SESSION_TYPE'));
  15.   //if SessionType = 'wayland' then setenv('QT_QPA_PLATFORM', 'xcb', 1);
  16.  
  17.   setenv('QT_QPA_PLATFORM', 'xcb', 1);
  18.   {$ENDIF}
  19.  

Code: [Select]
ShowMessage(GetEnvironmentVariable('XDG_SESSION_TYPE')+' --- '+GetEnvironmentVariable('QT_QPA_PLATFORM'));

if I compile and run I get "wayland --- ", it doesn't matter with both functions.

Alexx2000

  • New Member
  • *
  • Posts: 28
Re: Qt does not position forms
« Reply #18 on: May 12, 2026, 09:41:27 pm »
Yes, of course. Because GetEnvironmentVariable returns Free Pascal environment. While we change C  environment which used by Qt. You can check which session really used by Qt using IsWayland function from QtInt unit.

systemgvp

  • New Member
  • *
  • Posts: 46
Re: Qt does not position forms
« Reply #19 on: May 12, 2026, 09:43:16 pm »
Unfortunately I'm an amateur programmer

systemgvp

  • New Member
  • *
  • Posts: 46
Re: Qt does not position forms
« Reply #20 on: May 12, 2026, 09:56:34 pm »
I'm using this solution which actually sets the variable QT_QPA_PLATFORM=xcb

Code: Pascal  [Select][+][-]
  1. {$IFDEF LINUX}
  2. function setenv(const name, value: pchar; overwrite: cint): cint; cdecl; external clib;
  3. var SessionType: String;  EnvP: PPChar; external name 'environ';
  4. function SetEnvironmentVariable(const Name, Value: string): Boolean;
  5. begin
  6.   if setenv(PChar(Name), PChar(Value), 1) <> 0 then
  7.   begin
  8.     Result := False;
  9.     Exit;
  10.   end;
  11.   // Forza FPC a rileggere l'ambiente dalla libc
  12.   // Punta il puntatore interno di FPC all'environ della libc
  13.   System.envp := EnvP;
  14.   Result := True;
  15. end;
  16. {$ENDIF}
  17.  
  18. {$IFDEF LINUX}
  19.     //per risolvere il problema dei QT6 su wayland
  20.     SessionType := LowerCase(GetEnvironmentVariable('XDG_SESSION_TYPE'));
  21.     if SessionType = 'wayland' then SetEnvironmentVariable('QT_QPA_PLATFORM','xcb');
  22.   {$ENDIF

But I always get the same result. If I start the application from lazarus (run), where the variable is preset, the form position is displayed correctly. If I start the application outside of lazarus, the application sets the variable correctly but doesn't move the form.

dbannon

  • Hero Member
  • *****
  • Posts: 3826
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Qt does not position forms
« Reply #21 on: May 14, 2026, 08:49:53 am »
Well, I tried Alexx2000's method and found it worked perfectly, both in Lazarus and in a stand alone app running on a Ubuntu26.04 VM.

However, we must remember that libxcb is just an interface to X as is xlib. The nasty people who want to get rid of X will obviously remove libxcb when they finally have their way.

I tested my app on that U26.04 box, patched and it works fine, windows appear where they should be etc. So I then hid libxcb and my patched app crashed because it cannot find libxcb, I'd hoped it would fall back to xlib or even wayland.

So I tried an unpatched version, with libxcb hidden, it too crashed !  That means, even apps force to go via wayland have a dependency on libxcb ??  So, I read that as saying we are safe having a hard dependency on libxcb for the time being !

Better code might first test if libxcb is available before setting that env var, only a few more lines of code ...

IMHO.

Davo

 
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

zeljko

  • Hero Member
  • *****
  • Posts: 1951
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Qt does not position forms
« Reply #22 on: May 14, 2026, 09:08:15 am »
all qts in lazarus (qtint.pas) includes xlib unit and that's why it crashes when there's no any x11 lib on system afair. Probably I'll create an small unit with dynamic loading of libxcb (only needed stuff) to get x11 part work (qtx11.inc), but to avoid crash on pure wayland system without any x11 lib.

dbannon

  • Hero Member
  • *****
  • Posts: 3826
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Qt does not position forms
« Reply #23 on: May 14, 2026, 09:42:44 am »
Well, I have not seen a system yet that does not have X at all. U26.04 does not let you switch Gnome to X but all the expected X libs are still there. But for how long I ask ?

Mind you, looking at the size of the U26.04 install image, they clearly don't care about disk space ....

As usual zeljko, thanks for what you do !

Davo
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018