1
votes

I have a simple use case in a Qt app:

When the UI has been constructed and a QGraphicsView (or any other QWidget) has been populated, scroll the graphics view to a given location, based on the graphics views width and height (or any other attribute set by the layout engine).

However I can't find a reliable or known place to do this. In the constructors the layout has not yet been applied, so the width isn't the final width that the user sees once the UI is "up". Most suggestions seem to consist of hacks such as:

QTimer::singleShot(0, this, SLOT(initWidget()));

In my case this doesn't seem to work unless I set the delay to around 10msec which seems even more risky and hacky. Surely there must be something like win32's WM_INITDIALOG or the likes?

1
What is wrong about the single shot static timer functionality? It looks good to me. I personally do not consider it as hack.lpapp
Because sometimes it doesn't work, I have to try 10,20,30msec etc, Its like adding a sleep() to fix a race condition. There must be something in the framework that supports this. Maybe a "layout complete" event or something, I dunno..paulm
Well, just choose the biggest, but you could reimplement the showEvent(), too, as per the answer in here.lpapp

1 Answers

1
votes

You can override showEvent() or resizeEvent() of the relevant class.

After a bit of experimenting, since you say you're using showMaximized(), you actually may want to override both, and then get the size from first resizeEvent() after first showEvent() (or 2nd resizeEvent() overall). Output from my test app which used showMaximized():

Starting /home/hyde/test/build-eventTest-Desktop-Debug/eventTest...
resizeEvent size QSize(200, 100) oldSize QSize(-1, -1) 
showEvent geometry QRect(0,0 200x100) 
resizeEvent size QSize(1440, 851) oldSize QSize(640, 480) 
/home/hyde/test/build-eventTest-Desktop-Debug/eventTest exited with code 0

Then do what you need to do there. If you want to do it just once, then add a boolean member variable like

bool mFullyInited;

and initialize it to false, and then in the overriden event handler method, test it and set it to true when you've done the initialization.