我使用graphics view
framework写了一个功能模块,可以做
放大,缩小和拖动操作。但是当
图片放得比较大时,拖动就变得很吃力,图片有81K。
代码如下:
Map::Map(QWidget* parent) : QWidget(parent)
{
isShow = true;
worldMap = new QPixmap(":/images/map.png", "png");
qDebug("worldMap.width, height: %d, %d", worldMap->width(), worldMap->height());
scene = new QGraphicsScene(0, 0, worldMap->width(), worldMap->height());
scene->setBackgroundBrush(Qt::red);
/* add map to scene */
mapItem = scene->addPixmap(*worldMap);
qDebug() << "mapItem boundingRect" << mapItem->boundingRect();
mapItem->setFlags(QGraphicsItem::ItemIsMovable);
//mapItem->setTransformationMode(Qt::FastTransformation);
//mapItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
// this cache mode
make drag the picture fast
mapItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
/* add station node to scene 这是加了一些item进来*/
drawStationNode();
view = new BncMapView(this);
view->setScene(scene);
view->setSceneRect(0, 0, 200, 100);
// let the view fit the
widget automatically
QVBoxLayout* vLayout = new QVBoxLayout;
vLayout->addWidget(view);
setLayout(vLayout);
createActions();
//slotFitWidget();
updateAct();
}
自定义的view类是这样的:
class BncMapView : public QGraphicsView
{
Q_OBJECT
public:
BncMapView(QWidget *parent = 0);
protected:
void wheelEvent(QWheelEvent *event);
};
BncMapView::BncMapView(QWidget *parent)
: QGraphicsView(parent)
{
setDragMode(ScrollHandDrag);
setBackgroundBrush(Qt::black);
}
void BncMapView::wheelEvent(QWheelEvent *event)
{
double numDegrees = -event->delta() / 8.0;
double numSteps = numDegrees / 15.0;
double factor = std::pow(1.125, numSteps);
scale(factor, factor);
}
请大侠们赐教。谢谢
[ 此帖被yangguosdxl在2010-09-06 09:01重新编辑 ]