[英]QGraphicsView fit any size Image without scrollbar
我的工作环境: Qt 5.8 MSVC2015 64位,QT GraphicsView,QGraphicsObject,Windows 7 64位。
我正在使用QImage加载任何大小的图像,请尝试适合固定大小(宽200 X高200)的QGraphicsView。 我不想在QGraphicsView中使用滚动条。
在下面的示例中:
那么,如何将图像固定在固定大小的QGraphicsView中呢?
QImage *ImageData;
QGraphicsView* _ThumbNailView = new QGraphicsView(this);
_ThumbNailView->setFixedSize(200, 200); //QGraphicsView will be alwyas constant.
QGraphicsScene* _scene = new QGraphicsScene();
_scene->setSceneRect(0,0,200,200);
..........
myQGraphicsItem* _thumbsquare = new myQGraphicsItem(imageWidth, imageHeight, ImageData);
//Load image from buffer
unsigned char *buffer; ////some image Data get loaded here.
int imageWidth = 182; //I am getting image Width 182, or any size.
int imageHeight = 174; //I am getting image Height 174 or any size.
size_t size = imageWidth * imageHeight * 3;
int bytesPerLine = size / imageHeight;
QImage* _image = new QImage(reinterpret_cast<const uchar *>(buffer),182, 174, bytesPerLine, QImage::Format_RGB888);
_thumbsquare->setMyImage(QImage);
...........
int width = _ThumbNailView->geometry().width(); // always const 200
int height = _ThumbNailView->geometry().height(); // always const 200
_ThumbNailView->resize(width, height);
_scene->addItem(_thumbsquare);
_scene->setSceneRect(_scene->itemsBoundingRect());
// This don't work, make image very small
//_ThumbNailView->fitInView(QRectF(0, 0, 200, 200));
以上代码结果
没有滚动条的预期全尺寸图像
任何建议或帮助都受到高度赞赏?
@ Eligijus,谢谢您的帮助,它可以帮助我找到解决方案。
我的代码更改:
QRectF bounds = _scene->sceneRect();
QRectF rect {0,0,200,200};
if (bounds.width() < 200)
{
rect .setWidth(bounds.width());
bounds.setWidth(200);
}
if (bounds.height() < 200)
{
rect.setWidth(bounds.height());
bounds.setHeight(200);
}
_ThumbNailView->fitInView(bounds, Qt::KeepAspectRatio);
QRectF changeRect = _scene->sceneRect();
_ThumbNailView->updateGeometry();
但是,如果图像尺寸小于200,那么您将面临问题,否则事情将会顺利进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.