• 10780阅读
  • 2回复

GraphicsItemChange和QTransform的问题 [复制链接]

上一主题 下一主题
离线xuxinshao
 

只看楼主 倒序阅读 楼主  发表于: 2009-09-29
GraphicsItemChange和QTransform的问题
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
我想限制QGraphicsRectItem只能够在QGraphicsScene的sceneRect内移动,但是设置了QTransform后有问题,我的代码如下:
test.h
  1. #ifndef _TEST_H
  2. #define _TEST_H
  3. #include <QGraphicsRectItem>
  4. #include <QGraphicsScene>
  5. class Rectangle: public QGraphicsRectItem
  6. {
  7. public:
  8.     Rectangle( QGraphicsItem *parent = 0 );
  9. protected:
  10.     virtual QVariant itemChange( GraphicsItemChange change, const QVariant &value );
  11. };
  12. class Scene : public QGraphicsScene
  13. {
  14.     Q_OBJECT
  15. public:
  16.     Scene( QObject *parent = 0 );
  17. protected:
  18.     virtual void drawBackground( QPainter *, const QRectF & );
  19. };
  20. #endif


test.cpp文件
  1. #include "test.h"
  2. #include <QPainter>
  3. #include <QtDebug>
  4. Rectangle::Rectangle( QGraphicsItem *parent )
  5.         : QGraphicsRectItem( parent )
  6. {
  7.     setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable );
  8.     setBrush( Qt::red );
  9. }
  10. QVariant Rectangle::itemChange( GraphicsItemChange change, const QVariant &value )
  11. {
  12. if ( change == ItemPositionChange && scene() )
  13.     {
  14.         QPointF newPos = value.toPointF();
  15.         QRectF rect = scene()->sceneRect();
  16.         QRectF sbRect = sceneBoundingRect()
  17.         if ( !rect.contains( newPos ) )
  18.         {
  19.             qreal x = qMin( rect.right() - boundingRect().width(), qMax( sbRect.left(), rect.left() ) );
  20.             newPos.setX( x - boundingRect().x() );
  21.             qreal y = qMin( rect.bottom() - boundingRect().height(), qMax( sbRect.top(), rect.top() ) );
  22.             newPos.setY( y - boundingRect().y() );
  23.            
  24.             return newPos;
  25.         }
  26.     }
  27.     return QGraphicsItem::itemChange( change, value );[/color]
  28. }
  29. Scene::Scene( QObject *parent )
  30.         : QGraphicsScene( parent )
  31. {
  32. }
  33. void Scene::drawBackground( QPainter *p, const QRectF &r )
  34. {
  35.     p->save();
  36.     p->setPen( Qt::NoPen );
  37.     p->setBrush( QColor( 92, 92, 129 ) );
  38.     p->drawRect( r );
  39.     p->restore();
  40.     p->save();
  41.     p->setPen( Qt::black );
  42.     p->setBrush( QColor( 182, 182, 180 ) );
  43.     p->drawRect( sceneRect() );
  44.     p->restore();
  45.     p->save();
  46.     p->setPen( Qt::green );
  47.     QPointF center = sceneRect().center();
  48.     QPointF p1 = center; p1 += QPointF( -sceneRect().width()/2, 0 );
  49.     QPointF p2 = center; p2 += QPointF( sceneRect().width()/2, 0 );
  50.     p->drawLine( p1, p2 );
  51.     p1 = center; p1 += QPointF( 0, -sceneRect().height()/2 );
  52.     p2 = center; p2 += QPointF( 0, +sceneRect().height()/2 );
  53.     p->drawLine( p1, p2 );
  54.     p->restore();
  55. }


main.cpp
  1. #include "test.h"
  2. #include <QtGui>
  3. int main( int argc, char **argv )
  4. {
  5.     QApplication app( argc, argv );
  6.     Scene scene;
  7.     scene.setSceneRect( -160, -120, 320, 240 );
  8.     Rectangle *mouse = new Rectangle;
  9.     mouse->setRect( 0, 0, 40, 20 );
  10.     mouse->setPos( -160, -120 );
  11.     scene.addItem( mouse );
  12.    QPointF center = mouse->boundingRect().center();
  13.     mouse->setTransform( QTransform().translate( center.x(), center.y() ).rotate( 60 ).translate( -center.x(), -center.y() ), true );[/color]
  14.     QGraphicsView view;
  15.     view.setScene( &scene );
  16.     view.resize( 640, 480 );
  17.     view.show();
  18.     return app.exec();
  19. }

离线407920168
只看该作者 1楼 发表于: 2010-11-24
问题应该是boundingRect()的坐标是固定的 不随着setTramsform的变化而发生改变,
可以重新构造个QRectF rect, 通过获得Item的tranform,在使用QTranform类中的mapRect()来得到,
然后设置newPos.setX( x - rect.x()) 来得到新的newPos
离线sbtree
只看该作者 2楼 发表于: 2010-11-24
itemChange函数的内层if语句之后缺少返回值
windows 7 + VC++2008 + Qt4.5.2
快速回复
限100 字节
 
上一个 下一个