PDA

View Full Version : QDialog resizeEvent not happening in time?



Urthas
15th September 2010, 02:49
Hello,

In trying to respond to a recent thread in this forum (http://www.qtcentre.org/threads/31866-How-to-render-QGraphicsScene-View-to-FullScreen) I wrote the following code:

dialog.cpp


#include "dialog.h"
#include <QtGui>
#include <QDebug>

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
scene = new QGraphicsScene();
view = new QGraphicsView(scene, this);
}

void Dialog::resizeEvent(QResizeEvent *e)
{
qDebug() << "Dialog::resizeEvent()" << e->size();
view->resize(e->size());
scene->setSceneRect(view->viewport()->rect());
scene->clear();
scene->addRect(0, 0, scene->sceneRect().width()-1, scene->sceneRect().height()-1);
QDialog::resizeEvent(e);
}


Adding a rectItem to the scene after it gets resized is simply to show myself that everything is sized as it should be. And this solution seems to work brilliantly if main.cpp looks like this:



#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;

w.show();
w.resize(500, 500);
return a.exec();
}


With that code, I get the following debug output:



Dialog::resizeEvent() QSize(131, 30)
Dialog::resizeEvent() QSize(500, 500)


However, if the order of the method calls on w is reversed, I end up with only the following debug output:



Dialog::resizeEvent() QSize(500, 500)


and get a small rectangle in the center of the dialog. Then, when I manually resize the window by even one pixel, the rectangle snaps into place and everything works from then on.

Now, according to the documentation for QWidget::resize(),


If the widget is visible when it is being resized, it receives a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown.

Based on what I see, I'm clearly not understanding what's going on here. That is, the event *is* being received but is not reflected in the output. Hopefully a good night's sleep will make all the pieces click but in the mean time I'm going crazy here. Can someone *please* try to explain why my little proof-rectangle apparently isn't being updated in time (or whatever this is)? Thanks a lot!

tbscope
15th September 2010, 05:46
Use a layout instead of manually resizing the widgets inside your dialog.

As for the resize event.


w.show();
// Is a size already set?
// If not, use a default size.
// A resize event happens with the default size 131, 30

w.resize(500, 500);
// Resize the dialog
// A resize event happens with the set size.


w.resize(500, 500);
// Resize the dialog
// A resize event happens with the set size.

w.show();
// Is a size already set?
// If yes, show with the set size.
// A resize event is not needed, the dialog already has the correct size.


Edit:
Your use of the scene and the rectangle item isn't really correct either.

Urthas
15th September 2010, 06:05
The problem is that the dialog is being shown with the set size, and the view is also the set size, but (as further debug() statements have revealed*), the viewport's size is NOT reflective of the newly acquired view size! So I guess my question can be distilled down to the following:

When a call to resize() precedes a call to show(), the view is properly resized, as requested. Why does the viewport not follow suit on this initial event, yet on manual resizing, both view and viewport are properly resized?

*


void Dialog::resizeEvent(QResizeEvent *e)
{
qDebug() << "Dialog::resizeEvent()" << e->size();
view->resize(e->size());
scene->setSceneRect(view->viewport()->rect());

qDebug() << "View: " << view->width() << "," << view->height();
qDebug() << "Viewport: " << view->viewport()->width() << "," << view->viewport()->height();
qDebug() << "Scene: " << scene->sceneRect().width() << "," << scene->sceneRect().height();

scene->clear();
scene->addRect(0, 0, scene->sceneRect().width()-1, scene->sceneRect().height()-1);
QDialog::resizeEvent(e);
}


on show():
View: 500 , 500
Viewport: 98 , 28
Scene: 98 , 28

on subsequent manual resize, 1 pixel to the right:
View: 501 , 500
Viewport: 499 , 498
Scene: 499 , 498

Urthas
15th September 2010, 06:11
Your use of the scene and the rectangle item isn't really correct either.

Ordinarily I would not add to the scene in the Dialog's resizeEvent(). It's there for purposes of quick-and-dirty visualization.