2017-07-07 26 views
0

私の仕事環境: Qt 5.8 MSVC2015 64ビット、QT GraphicsView、QGraphicsObject、Windows 7 64ビット。QGraphicsViewはスクロールバーなしの画像

QImageを使用して任意のサイズの画像をロードしています。 固定サイズのQGraphicsViewに試着してください(幅200×高さ200)。私はQGraphicsViewでスクロールバーをしたくありません。例以下で

  1. マイQGraphicsViewサイズ常に幅200 X高さを修正することになる200
  2. 私の画像サイズは変更される場合がありますが、コードの下で私のイメージ 幅182 *高さ174。

どのように私はどのようにフィットすることができますか固定サイズの画像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)); 

コード結果上記

Above Code OutPut

期待のフルフィットイメージスクロールバー

Image Without Scroll bar

せずに任意の提案やヘルプは非常aはppreciated?

+2

[Qt5 C++ QGraphicsView:画像がビューフレームに適合しません](https://stackoverflow.com/questions/17028680/qt5-c-qgraphicsview-images-dont-fit-view-frame) –

+0

@ Eligijus:スクロールバーの問題を解決する上記のリンクを試しました。しかし、画像幅が200未満の場合は、200幅を完全に占有しません。 QRectF bounds = _scene-> sceneRect(); if(bounds.width()<200) { bounds.setWidth(200); } if(bounds.height()<200) { bounds.setHeight(200); } _ThumbNailView-> fitInView(bounds、Qt :: KeepAspectRatio); – Sandy

答えて

0

@ 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よりも画像サイズより少ない場合、その後、あなたが問題に直面するだろう、それ以外の事はスムーズになります。

関連する問題