Please check the following implementation:
BoardView.h
#include <QGraphicsView>
class BoardView : public QGraphicsView
{
protected:
void resizeEvent(QResizeEvent* event);
public:
BoardView();
};
BoardView.cpp
BoardView::BoardView()
{
}
void BoardView::resizeEvent(QResizeEvent* event)
{
QGraphicsView::resizeEvent(event);
double wd = width();
double ht = height();
double min_wh = min(wd, ht);
qDebug() << min_wh;
QTransform transform;
transform.scale(min_wh / Options::getMainBoardMinSize(), min_wh / Options::getMainBoardMinSize());
setTransform(transform);
}
The above two files are directly copied from an existing project, say P1. The purpose of this code is to handle resizeEvent()
and scale the view according to widget size.
The problem is, this works exactly as it should in project P1. Everything is scaled as expected. But resizeEvent()
is not called from the new project at all, when the view is resized by resizing main window, on which it is a widget.
The BoardView object d开发者_运维百科oes everything else as expected. It shows the scene. It captures and passes mouse events to the scene etc. Only the resizeEvent()
is not called.
What am I doing wrong?
I'm using Qt Creator 2.0.1, Qt 4.7 32 bit on Windows 7 Ultimate 32 bit.
Hmm... Are you missing some code here? Or your class declaration is incorrect - you should change it to
class BoardView : public QGraphicsView
{
Q_OBJECT
...
public:
BoardView(QWidget* parent);
};
...
BoardView::BoardView(QWidget* parent):QGraphicsView(parent)
{
}
Sorry if this is omitted by you because of triviality, but I don't know your QT experience level. This might be why QT events works incorrectly with your class...
Why a BoardView::resizeEvent()
is not called, yet the QGraphicsView
was showing the QGraphicsScene
correctly? The reason is a silly mistake, instead of
BoardView _board_view;
I wrote
QGraphicsView _board_view;
Sorry for posting such a silly question!
精彩评论