4

I'm trying to create a QMainWindow to encapsulate a QGraphicsView that I'm putting in it. I want it to start out maximized, so I do this:

QMainWindow *mainWindow = new QMainWindow();
mainWindow->setWindowState(Qt::WindowMaximized);
mainWindow->show();
qDebug() << mainWindow->size();

Which says my maximized window is 200x100, which is obviously incorrect.

Am I missing some sort of update function? I don't get why it wouldn't update the size. I've also tried using showMaximized() with the same result.

EDIT

My end-goal is to use the QMainWindow as a container for a QGraphicsView containing a QGraphicsScene. On top of all this, I want to have a QWebView at 50% width and 100% height, centered over everything.

So, I need the width and height in order to get all the coordinates and sizes correct.

K. Barresi
  • 1,275
  • 1
  • 21
  • 46
  • 1
    I'm guessing this is part of you `main` or similar, i.e. before the event loop even starts. You'll need to wait for events to be processed for the window's size to be adjusted - I think `show` just posts an event. – Mat May 14 '13 at 20:46
  • Yes, it's right at the top of `main`. Is there a recommended way to let everything settle before I can see the updated size? – K. Barresi May 14 '13 at 20:48
  • Depends on what you need that size for, I guess. Why do you need the size? – Mat May 14 '13 at 20:50
  • I have a QGraphicsView with a QGraphicsScene that I want maximized in it, and then attach a QWebView at 50% width and 100% height, centered over everything. – K. Barresi May 14 '13 at 20:51
  • Then you should update your question to explain that that's what you're after. Doesn't sound trivial - having the view resize with it's parent should be (and shouldn't require anything more than a standard layout), but the 50% width thing I'm not too sure. Might need to handle `resizeEvent`, or spacers, or something. – Mat May 14 '13 at 20:54
  • I figured once I was able to get everything correct statically, handling the `resizeEvent` wouldn't be too tough, as long as I have correct/up to date width/heights :) – K. Barresi May 14 '13 at 20:57

1 Answers1

2

Well, the effect of setWindowState() is not immediate, it gets executed asynchronously. When the window state changes, the widget receives a changeEvent(), so you should reimplement this or resizeEvent() to get the width() and height() after the maximization takes place.

vMTom
  • 21
  • 3