• 6657阅读
  • 1回复

请教用graphics view框架显示图片的问题 [复制链接]

上一主题 下一主题
离线yangguosdxl
 

只看楼主 倒序阅读 楼主  发表于: 2010-09-01
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
我使用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重新编辑 ]
离线yangguosdxl

只看该作者 1楼 发表于: 2010-09-02
怎么没有人回答呢?是我没有说清楚吗?
快速回复
限100 字节
 
上一个 下一个