PDA

View Full Version : QGraphicsView coordinates transformation



mtribaldos
6th February 2008, 17:21
Hi,

I'm trying to implement a calendar-like widget using the QGraphicsView and
QGraphicsScene classes. I think they provide some very useful features, such as item collisions, for my application. The problem comes when adjusting the window size. I need to adjust the grid space for a week view (monday, tuesday,...) to the window width, without horizontal scrollbar, but not for the window height (the vertical scrollbar are ok).

The only idea in my mind is to clear the scene and redraw the item in it,
according to the new size (only width) of the scene. How could I do it better?
Some idea to do this window-viewport transformation, only in horizontal?

Thank you in advance.

marcel
6th February 2008, 17:44
I think the best example is the Qt Demo application.
Take a look at this:


void MainWindow::resizeEvent(QResizeEvent *event)
{
this->resetMatrix();
this->scale(event->size().width() / 800.0, event->size().height() / 600.0);
QGraphicsView::resizeEvent(event);
DemoItem::setMatrix(this->matrix());

if (this->trolltechLogo){
const QRectF r = this->scene->sceneRect();
QRectF ttb = this->trolltechLogo->boundingRect();
this->trolltechLogo->setPos(int((r.width() - ttb.width()) / 2), 595 - ttb.height());
QRectF qtb = this->qtLogo->boundingRect();
this->qtLogo->setPos(802 - qtb.width(), 0);
}

// Changing size will almost always
// hurt FPS during the changing. So
// ignore it.
this->fpsHistory.clear();
}

This is the resize event of the main window, which is also a QGraphicsView.
The first 3 lines are the most relevant.

Regards