我有一个 QGraphicsView,因为我有一个 QGraphicsScene,因为我有一个 QLabel,并且我将 .png 图片作为 QPixmap 设置到 QLabel 中。.png 在 background.qrc 资源文件中设置。我的 QLabel 的尺寸是 600x400。没有像素图也没关系,QGraphicsScene 的大小也是 600x400。但是当我将像素图设置为 QLabel 并对其进行缩放时,它失败了。QLabel 的大小是一样的,像素图在 QLabel 内缩放得很好,并且只在其中可见,但是 QGraphicsScene 采用了 QPixmap 的实际大小,即 720x720。所以 QLabel 在 QPixmap 的大小正确时是可见的,但它周围有一个灰色的地方,因为场景更大。我该如何解决这个问题并让它工作?我希望 QGraphicScene 保持 QLabel 的大小。
这是代码:
#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;
}