-
UID:287
-
- 注册时间2005-08-21
- 最后登录2018-01-13
- 在线时间138小时
-
- 发帖124
- 搜Ta的帖子
- 精华0
- 金钱1024
- 威望147
- 贡献值10
- 好评度89
-
访问TA的空间加好友用道具
|
GraphicsItemChange和QTransform的问题
—
本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02)
—
我想限制QGraphicsRectItem只能够在QGraphicsScene的sceneRect内移动,但是设置了QTransform后有问题,我的代码如下: test.h - #ifndef _TEST_H
- #define _TEST_H
- #include <QGraphicsRectItem>
- #include <QGraphicsScene>
- class Rectangle: public QGraphicsRectItem
- {
- public:
- Rectangle( QGraphicsItem *parent = 0 );
- protected:
- virtual QVariant itemChange( GraphicsItemChange change, const QVariant &value );
- };
- class Scene : public QGraphicsScene
- {
- Q_OBJECT
- public:
- Scene( QObject *parent = 0 );
- protected:
- virtual void drawBackground( QPainter *, const QRectF & );
- };
- #endif
test.cpp文件 - #include "test.h"
- #include <QPainter>
- #include <QtDebug>
- Rectangle::Rectangle( QGraphicsItem *parent )
- : QGraphicsRectItem( parent )
- {
- setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable );
- setBrush( Qt::red );
- }
- QVariant Rectangle::itemChange( GraphicsItemChange change, const QVariant &value )
- {
- if ( change == ItemPositionChange && scene() )
- {
- QPointF newPos = value.toPointF();
- QRectF rect = scene()->sceneRect();
- QRectF sbRect = sceneBoundingRect()
- if ( !rect.contains( newPos ) )
- {
- qreal x = qMin( rect.right() - boundingRect().width(), qMax( sbRect.left(), rect.left() ) );
- newPos.setX( x - boundingRect().x() );
- qreal y = qMin( rect.bottom() - boundingRect().height(), qMax( sbRect.top(), rect.top() ) );
- newPos.setY( y - boundingRect().y() );
-
- return newPos;
- }
- }
- return QGraphicsItem::itemChange( change, value );[/color]
- }
- Scene::Scene( QObject *parent )
- : QGraphicsScene( parent )
- {
- }
- void Scene::drawBackground( QPainter *p, const QRectF &r )
- {
- p->save();
- p->setPen( Qt::NoPen );
- p->setBrush( QColor( 92, 92, 129 ) );
- p->drawRect( r );
- p->restore();
- p->save();
- p->setPen( Qt::black );
- p->setBrush( QColor( 182, 182, 180 ) );
- p->drawRect( sceneRect() );
- p->restore();
- p->save();
- p->setPen( Qt::green );
- QPointF center = sceneRect().center();
- QPointF p1 = center; p1 += QPointF( -sceneRect().width()/2, 0 );
- QPointF p2 = center; p2 += QPointF( sceneRect().width()/2, 0 );
- p->drawLine( p1, p2 );
- p1 = center; p1 += QPointF( 0, -sceneRect().height()/2 );
- p2 = center; p2 += QPointF( 0, +sceneRect().height()/2 );
- p->drawLine( p1, p2 );
- p->restore();
- }
main.cpp - #include "test.h"
- #include <QtGui>
- int main( int argc, char **argv )
- {
- QApplication app( argc, argv );
- Scene scene;
- scene.setSceneRect( -160, -120, 320, 240 );
- Rectangle *mouse = new Rectangle;
- mouse->setRect( 0, 0, 40, 20 );
- mouse->setPos( -160, -120 );
- scene.addItem( mouse );
- QPointF center = mouse->boundingRect().center();
- mouse->setTransform( QTransform().translate( center.x(), center.y() ).rotate( 60 ).translate( -center.x(), -center.y() ), true );[/color]
- QGraphicsView view;
- view.setScene( &scene );
- view.resize( 640, 480 );
- view.show();
- return app.exec();
- }
|