Hello,
In trying to respond to a recent thread in this forum I wrote the following code:
dialog.cpp
#include "dialog.h"
#include <QtGui>
#include <QDebug>
{
}
{
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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
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[])
{
Dialog w;
w.show();
w.resize(500, 500);
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
With that code, I get the following debug output:
Dialog
::resizeEvent() QSize(131,
30) Dialog
::resizeEvent() QSize(500,
500)
Dialog::resizeEvent() QSize(131, 30)
Dialog::resizeEvent() QSize(500, 500)
To copy to clipboard, switch view to plain text mode
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)
Dialog::resizeEvent() QSize(500, 500)
To copy to clipboard, switch view to plain text mode
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!
Bookmarks