用qt写一个图片外接框,通过拖动外接框实现对图片大小的缩放
时间: 2023-05-17 19:05:20 浏览: 86
可以使用QGraphicsView和QGraphicsPixmapItem来实现这个功能。首先,创建一个QGraphicsView对象,然后将其设置为可拖动和可缩放。接下来,创建一个QGraphicsPixmapItem对象,并将其添加到QGraphicsView中。最后,将QGraphicsView显示出来即可。
以下是示例代码:
```cpp
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QPixmap pixmap("image.jpg");
QGraphicsPixmapItem *item = scene.addPixmap(pixmap);
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setFlag(QGraphicsItem::ItemIsSelectable);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
item->setFlag(QGraphicsItem::ItemIsFocusable);
view.setDragMode(QGraphicsView::ScrollHandDrag);
view.setRenderHint(QPainter::Antialiasing);
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
view.setBackgroundBrush(QBrush(Qt::gray));
view.setFrameStyle(QFrame::NoFrame);
view.setInteractive(true);
view.show();
return app.exec();
}
```
这段代码创建了一个QGraphicsView对象,并将其设置为可拖动和可缩放。然后,创建了一个QGraphicsPixmapItem对象,并将其添加到QGraphicsView中。最后,将QGraphicsView显示出来。
阅读全文
相关推荐
















