#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPen>
#include <QWheelEvent>
#include <cmath>
#include <QDebug>
public:
protected:
double scaleFactor = pow(2.0, event->delta() / 240.0);
scale(scaleFactor, scaleFactor);
}
};
int main(int argc, char *argv[])
{
scene.setSceneRect(0, 0, 800, 800);
rectItem->setPos(200, 200);
rectItem
->setBrush
(QColor(255,
0,
0));
scene.addItem(rectItem);
QFont f
= nameItem
->font
();
f.setPointSize(12);
nameItem->setFont(f);
nameItem->setPos(rectItem->rect().center());
MainView view(&scene);
view.show();
return a.exec();
}
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPen>
#include <QWheelEvent>
#include <cmath>
#include <QDebug>
class MainView : public QGraphicsView {
public:
MainView(QGraphicsScene *scene) : QGraphicsView(scene) { setBackgroundBrush(QBrush(QColor(255, 255, 255)));}
protected:
void wheelEvent(QWheelEvent *event) {
double scaleFactor = pow(2.0, event->delta() / 240.0);
scale(scaleFactor, scaleFactor);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
scene.setSceneRect(0, 0, 800, 800);
QGraphicsRectItem* rectItem = new QGraphicsRectItem(QRectF(0, 0, 400, 200));
rectItem->setPos(200, 200);
rectItem->setBrush(QColor(255, 0, 0));
scene.addItem(rectItem);
QGraphicsSimpleTextItem *nameItem = new QGraphicsSimpleTextItem("name", rectItem);
QFont f = nameItem->font();
f.setPointSize(12);
nameItem->setFont(f);
nameItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
nameItem->setPos(rectItem->rect().center());
MainView view(&scene);
view.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks