My work Environment:QT5.8 MSVC2015 64位,QT GraphicsView,QGraphicsRectItem,Windows 7 64位。
问题:当我放大和放大GraphicsView时,我称之为GraphicsView缩放方法。但是当我将QGraphicsRectItem添加到场景中时,它无法调用它的画图方法。
我的班级等级:
class GraphicsView : public QGraphicsView
class mySquare : public QObject, public QGraphicsRectItem
class Scene : public QGraphicsScene 代码:
/* QGraphicsRectItem */
void mySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect(0,0,_width,_height);
painter->drawRect(rect);
}
void GraphicsView::wheelEvent(QWheelEvent * event)
{
int angle = event->angleDelta().y(); // angle will tell you zoom in or out.
double scaleFactor = 1.15; //How fast we zoom
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
if (angle > 0)
{
qDebug()<<"Zoom in";
scale(scaleFactor, scaleFactor);
}
else
{
qDebug()<<"Zoom out";
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
mySquare _square = new mySquare(0,blankImage);
_square->setPos(QPointF(0, 0));
//add square to scene.
//Bug!!! Why after scale function call, _square failed to draw rect in paint method.
_scene->addItem(_square);
_square->update();
}这是git 代码
我花了几天&几个小时,但仍然无法找出为什么它的QGraphicsRectItem无法打印,当刻度方法被调用。有什么建议值得高度赞赏吗?
发布于 2017-06-21 14:23:15
谢谢你的帮助。我通过重写QGraphicsView刻度方法找到了解决方案。更改:类mySquare : public QGraphicsObject
void GraphicsView::scale(qreal scaleFactor)
{
QRectF r(0, 0, 1, 1); // A reference
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(r).width(); // absolute zoom factor
( (rFactor > 0 )
{
qDebug()<<"Zoom in";
}
else
{
qDebug()<<"Zoom out";
}
mySquare _square = new mySquare(0,blankImage);
_square->setPos(QPointF(0, 0));
_scene->addItem(_square);
_square->update();
}
QGraphicsView::scale(scaleFactor, scaleFactor);
}https://stackoverflow.com/questions/44650059
复制相似问题