Retrieving exact Canvas location

Hallo,

After a program restart, I want to put a canvas at the exact same location as it was before, however, I have some issues to retrieve the exact location. For example, look at this code:

root [0] TCanvas* C = new TCanvas(“a”, “a”, 50, 50, 500, 500);
root [1] int TopX = C->GetWindowTopX();
root [2] int TopY = C->GetWindowTopY();
root [3] cout<<TopX<<", "<<TopY<<endl;
60, 111

The first line puts the canvas at location 50/50. However, when I try to retrieve that location, I get 60/111. The difference I assume is used up by the windows decorators. How do I get the true topX and topY location which I can use in the canvas constructor?

Thanks!


ROOT Version: 6.24
Platform: Ubuntu 20.04
Compiler: gcc 9.4


See the comment at the end of the header here: ROOT: TCanvas Class Reference
Set/GetCanvasSize Set/GetWindowSize that amyle help. I am not sure

@acz
On Ubuntu with WSL on Windows 10, your example actually produces the expected values for me, i.e. Top = 50,50.
But directly on ubuntu (Xubuntu), it shows the ‘wrong’ position. Since the difference depends on the window decorations, as couet shows, as a workaround one can just take the now known differences and subtract them from TopX and TopY. As long as you use the same machine and window theme, these values should not change.
By the way, to obtain these “known” differences, I would suggest to create the canvas farther from the screen edges (e.g. TCanvas* C = new TCanvas(“a”, “a”, 300, 300, 500, 500) in order to avoid possible automatic shifts if there isn’t enough space to create the canvas window at the requested position.
What seems strange is that your differences are too large (10 and 61), but maybe you are using a hi-dpi theme or something with very large window decorations; in my case, for instance, the differences are 4 and 23 pixels for x and y.

1 Like

@dastudillo
Thanks for the workaround! Determining the difference between the set location when the canvas is created and the retrieved location immediately afterwards, and then applying that difference later to the location of the recreated canvas is a simple solution which works nicely.

And yes, I have a hi-dpi theme on a laptop. That’s why the differences are large.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.