2016-08-01 2 views
0

나는 QGraphicsScene을 가지고 있는데, 나는 QLabel을 가지고 있고 QPixmap을 QLabel로 설정했다. .png는 background.qrc 리소스 파일에 설정됩니다. QLabel의 크기는 600x400입니다. 픽스맵이 없다면 QGraphicsScene의 크기도 600x400입니다. 그러나 픽스맵을 QLabel로 설정하고 크기를 조정하면 실패합니다. QLabel의 크기는 동일합니다. pixmap은 QLabel 내에서 크기가 조정되고 그 안에 만 표시되지만 QGraphicsScene은 720x720 인 QPixmap의 실제 크기를 채택합니다. QLabel은 정확한 크기의 QPixmap으로 볼 수 있지만 장면이 더 크기 때문에 주위에 회색 위치가 있습니다. 어떻게 해결하고 작동하게 할 수 있습니까? QGraphicScene이 QLabel의 크기를 유지하기를 원합니다. 당신이 장면의 크기를 설정하지 마십시오, 당신의 예제 코드에서QT QGraphicsScene with QLabel and QPixmap

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include <QPixmap> 
#include <QGraphicsView> 
#include <QGraphicsScene> 
#include <QLabel> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    QGraphicsView *myView = new QGraphicsView(this); 
    QGraphicsScene *myScene= new QGraphicsScene(); 
    QLabel *myLabel= new QLabel(); 
    myLabel->setBaseSize(QSize(600, 400)); 
    myLabel->resize(myLabel->baseSize()); 
    myLabel->setScaledContents(true); 
    QPixmap pixmapBackground(":/new/cross.png"); 
    myLabel->setPixmap(pixmapBackground); 
    myScene->addWidget(myLabel); 
    myView->setScene(myScene); 
    setCentralWidget(myView); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
+1

왜 대신'QGraphicsPixmapItem'을 사용하지 않으시겠습니까? 더 가볍고 제어하기 쉽습니다. – ilotXXI

+0

그것은 나를 위해 작동하지 않습니다. QLabel과 QPixmap으로 만들어야합니다. – Alex

+1

@Alex 그리고 "당신을 위해 일하지 않는다"는 것은 무엇을 의미합니까? 어쩌면 우리는이 문제를 해결할 수 있습니다. – Tomas

답변

1

:

여기에 코드입니다. setSceneRect로 전화하면됩니다. 문서가 명시한대로 rect가 설정되지 않은 경우 : -

null QRectF로 설정된 경우 sceneRect()는 장면이 생성 된 이후 장면의 모든 항목에 대해 가장 큰 경계 rect를 반환합니다 (즉, 항목이 장면에 추가되거나 이동할 때 커지지만 축소되지는 않는 사각형).

따라서 여기에서 입증 된 바와 같이 라벨을, 예

#include <QApplication> 
#include <QGraphicsView> 
#include <QGraphicsScene> 
#include <QLabel> 
#include <QPixmap> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    QGraphicsView *myView = new QGraphicsView; 
    QGraphicsScene *myScene= new QGraphicsScene(); 

    // constrain the QGraphicsScene size 
    // without this, the defect as stated in the question is visible 
    myScene->setSceneRect(0,0,600,400); 

    QLabel *myLabel= new QLabel;  
    myLabel->setBaseSize(QSize(600, 400)); 
    myLabel->resize(myLabel->baseSize()); 
    myLabel->setScaledContents(true); 

    QPixmap pixmapBackground(720, 720); 
    pixmapBackground.fill(QColor(0, 255, 0)); // green pixmap 

    myLabel->setPixmap(pixmapBackground); 
    myScene->addWidget(myLabel); 
    myView->setScene(myScene); 
    myView->show(); 

    return a.exec(); 
} 

이 정확한 장면 크기 초래한다 같이 그 크기가 변하는 장면에 추가 될 때, 장면 RECT 설정하지 않고 - 주석에서 설명하고있는 바와 같이

enter image description here

, 예제 코드는 OS X에서 예상대로 위의 작동하지만,에 실행할 때 여전히 문제가있을 나타납니다 Windows 10.

+0

나는 QPixmap을 추가하기 직전에 이것을 시도했다. 'myScene-> setSceneRect (0, 0, myLabel-> width(), myLabel-> height()); ' 결과는 동일하다. – Alex

+0

예를 들어 답변을 업데이트했습니다. 여기에서 볼 수 있듯이 * setSceneRect *가 호출되지 않으면보고있는 효과가 동일합니다. – TheDarkKnight

+0

이것은 정확하게 내가하려고했던 것이지만, 나는 그것이 작동하지 않는다고 말했다! [link] (https://postimg.org/image/lct8jh181/) 코드의 결과로 복사하여 붙여 넣습니다. 그것은 창 크기 나 시야의 크기를 바꾸지 않는 한 좋은 일이 될 것입니다 ... 물론, 나는이 작은 문제를 즉시 볼 수 있습니다. 하지만 저를 도와 주려고 시간을내어 주셔서 감사합니다 !! – Alex

관련 문제