KristianKarl
9th August 2010, 13:18
I have this piece of code:
#include <QtGui>
class GPS_Site : public QGraphicsEllipseItem {
public:
GPS_Site(const QString& text, qreal lon, qreal lat) {
setZValue(2);
QGraphicsEllipseItem::setFlags(ItemIgnoresTransfor mations | ItemIsSelectable);
QGraphicsEllipseItem::setPos(lon, lat);
QGraphicsEllipseItem::setRect(-5, -5, 10, 10);
QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(text, this);
label->setFlags(ItemIgnoresTransformations | ItemIsSelectable);
label->setPos(2, 3);
}
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow() {
QGraphicsScene* scene = new QGraphicsScene();
scene->addItem(new GPS_Site("Lower left: A very long text label, bla bla bla bla bla bla bla", 58.111, 24.111));
scene->addItem(new GPS_Site("Upper right: bla bla bla", 58.999, 24.999));
view = new QGraphicsView(scene);
view->setScene(scene);
QMatrix m(1.0, 0.0, 0.0, -1.0, 0.0, 0.0); // identity matrix with reversed Y-axis
view->setMatrix( m );
setCentralWidget(view);
}
protected:
virtual void wheelEvent(QWheelEvent* event) {
qreal scaleFactor = pow((double) 2, -event->delta() / 240.0);
view->scale(scaleFactor, scaleFactor);
}
private:
QGraphicsView* view;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow* mainWin = new MainWindow();
mainWin->show();
return app.exec();
}
The code, when run, depicts 2 geographical sites with gps coordinates, and labels.
The sites and labels are made scale invariant using ItemIgnoresTransformations.
The problem I have is, that the size set by QGraphicsScene is too large, and the reason for this are the long labels used. The rendered scene puts the sites too close to one another.
What I would like to achieve, is an initial scene size that big enough to encompass the 2 sites (ignoring the label items sizes and positions).
50485047
The first image shows the 1 case, the second shows something what I would like to have.
I have tried so many combinations of sceneRect, mapFromScene etc etc but with no proper solution.
Any tips or hints?
/Regards Kristian
#include <QtGui>
class GPS_Site : public QGraphicsEllipseItem {
public:
GPS_Site(const QString& text, qreal lon, qreal lat) {
setZValue(2);
QGraphicsEllipseItem::setFlags(ItemIgnoresTransfor mations | ItemIsSelectable);
QGraphicsEllipseItem::setPos(lon, lat);
QGraphicsEllipseItem::setRect(-5, -5, 10, 10);
QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(text, this);
label->setFlags(ItemIgnoresTransformations | ItemIsSelectable);
label->setPos(2, 3);
}
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow() {
QGraphicsScene* scene = new QGraphicsScene();
scene->addItem(new GPS_Site("Lower left: A very long text label, bla bla bla bla bla bla bla", 58.111, 24.111));
scene->addItem(new GPS_Site("Upper right: bla bla bla", 58.999, 24.999));
view = new QGraphicsView(scene);
view->setScene(scene);
QMatrix m(1.0, 0.0, 0.0, -1.0, 0.0, 0.0); // identity matrix with reversed Y-axis
view->setMatrix( m );
setCentralWidget(view);
}
protected:
virtual void wheelEvent(QWheelEvent* event) {
qreal scaleFactor = pow((double) 2, -event->delta() / 240.0);
view->scale(scaleFactor, scaleFactor);
}
private:
QGraphicsView* view;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow* mainWin = new MainWindow();
mainWin->show();
return app.exec();
}
The code, when run, depicts 2 geographical sites with gps coordinates, and labels.
The sites and labels are made scale invariant using ItemIgnoresTransformations.
The problem I have is, that the size set by QGraphicsScene is too large, and the reason for this are the long labels used. The rendered scene puts the sites too close to one another.
What I would like to achieve, is an initial scene size that big enough to encompass the 2 sites (ignoring the label items sizes and positions).
50485047
The first image shows the 1 case, the second shows something what I would like to have.
I have tried so many combinations of sceneRect, mapFromScene etc etc but with no proper solution.
Any tips or hints?
/Regards Kristian