QGraphicsView Scale&QGraphicsRectItem paint未能调用
问题描述:
我的工作环境:Qt 5.8 MSVC2015 64位,QT GraphicsView,QGraphicsRectItem,Windows 7 64位。QGraphicsView Scale&QGraphicsRectItem paint未能调用
发布时间:当我放大&缩小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();
}
这里是混帐code
我花天&小时,但仍然无法找到为什么QGraphicsRectItem无法打印,当调用缩放方法。任何建议是高度赞赏?
答
感谢您的帮助。我通过重写QGraphicsView缩放方法来找到解决方案。 变化: 类mySquare:公共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);
}
线 “mySquare _square =新mySquare(0,blankImage);”不会编译。请编辑您的问题以显示真实的代码。 –
更新的git代码url: https://github.com/SandyWare/ImageRender – Sandy
注意从'QObject'和'QGraphicsRectItem'扩展有一个用于这个特殊目的的类[QGraphicsObject](http://doc.qt .io/qt-5.9/qgraphicsobject.html) – Elia