Thank you amleto, nice to see you over here, too! Well, I don't know how to get the area - that's kind of my problem ;-). I thought the viewable size might be exactly that of the view. I've changed "view" to view->viewport():
rectItem->setRect(this->view->mapToScene(this->view->viewport()->rect()).boundingRect());
rectItem->setRect(this->view->mapToScene(this->view->viewport()->rect()).boundingRect());
To copy to clipboard, switch view to plain text mode
But it didn't change the result.
I have never set any translation on scene or view, thus I hadn't checked that and I'm not quite sure if I did it correctly now:
qDebug()<<this->view->transform();
qDebug()<<this->view->transform();
To copy to clipboard, switch view to plain text mode
yields this:
QTransform(type=TxNone, 11=1 12=0 13=0 21=0 22=1 23=0 31=0 32=0 33=1)
Because of "type=TxNone" I think this should be a non-transforming matrix. Thinking about it... it's a 3x3 matrix with diagonal "1"s, that's identity, of course. Long time since I last thought about that stuff... :-)
As for the compilable code: it's a >6000 lines project by now, so I have built a new project here that is compilable and seems the smallest size possible to reproduce my problem:
main.cpp:
#include <QtGui>
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow mainWin;
mainWin.show();
return app.exec();
}
#include <QtGui>
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGraphicsScene>
#include <QMainWindow>
{
Q_OBJECT
public:
MainWindow();
~MainWindow();
private:
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGraphicsScene>
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
~MainWindow();
private:
QGraphicsView *view;
QGraphicsScene *scene;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include <QtGui>
#include "mainwindow.h"
#include <qdebug.h>
MainWindow::MainWindow()
{
this->showMaximized();
setCentralWidget(widget);
widget->setLayout(layout);
layout->addWidget(view);
this->scene->setSceneRect(this->view->viewport()->rect());
rectItem->setRect(this->view->mapToScene(this->view->viewport()->rect()).boundingRect());
qDebug()<<this->view->transform();
scene->addItem(rectItem);
rectItem
->setBrush
(QBrush(Qt
::black,Qt
::SolidPattern));
}
MainWindow::~MainWindow()
{
delete scene;
}
#include <QtGui>
#include "mainwindow.h"
#include <qdebug.h>
MainWindow::MainWindow()
{
this->showMaximized();
scene=new QGraphicsScene(this);
view = new QGraphicsView(scene);
QHBoxLayout *layout = new QHBoxLayout;
QWidget *widget = new QWidget;
setCentralWidget(widget);
widget->setLayout(layout);
layout->addWidget(view);
this->scene->setSceneRect(this->view->viewport()->rect());
QGraphicsRectItem* rectItem = new QGraphicsRectItem();
rectItem->setRect(this->view->mapToScene(this->view->viewport()->rect()).boundingRect());
qDebug()<<this->view->transform();
scene->addItem(rectItem);
rectItem->setBrush(QBrush(Qt::black,Qt::SolidPattern));
}
MainWindow::~MainWindow()
{
delete scene;
}
To copy to clipboard, switch view to plain text mode
Instead of the scene being filled entirely by the black QGraphicsRectItem (as expected) the result of this "naked" example is this one:
tryoutResult.jpg
I've tried to follow this thread, but my result doesn't change, when I do the correspondend changes in my mainwindow.cpp:
QMatrix const matrix
= view
->matrix
().
inverted();
QRect visibleRect
= matrix.
mapRect(view
->viewport
()->rect
());
// when the viewport is scrolled, then the top left point of the visible rectangle needs to be moved
// according to the scroll bar positions
visibleRect.
moveTopLeft(matrix.
map(QPoint(view
->horizontalScrollBar
()->value
(),
view->verticalScrollBar()->value())));
rectItem->setRect(visibleRect);
QMatrix const matrix = view->matrix().inverted();
QRect visibleRect = matrix.mapRect(view->viewport()->rect());
// when the viewport is scrolled, then the top left point of the visible rectangle needs to be moved
// according to the scroll bar positions
visibleRect.moveTopLeft(matrix.map(QPoint(view->horizontalScrollBar()->value(),
view->verticalScrollBar()->value())));
QGraphicsRectItem* rectItem = new QGraphicsRectItem();
rectItem->setRect(visibleRect);
To copy to clipboard, switch view to plain text mode
I am quite desperate now. It seemed to work for shad0w, why not for me?
Thank you for your time!
Bookmarks