这两个代码段都加载到图像中。代码 1,加载图像并具有缩放功能,代码 2 应该只加载图像。代码 1 完美运行,但是当我尝试简化它时,我失去了加载图像功能。由于某种原因,图像在可见之前就被破坏了。
看起来它应该是相当直截了当的,但我似乎无法修复它。
代码 1:(这可行,但似乎过于复杂)
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
QImage image(":/images/2.png");
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsView* view = new QGraphicsView(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->setBackgroundBrush(QPixmap(":/images/2.png"));
scene->setBackgroundBrush(image.scaled(100,100,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(image).scaledToWidth(50));
QGraphicsEllipseItem *item2 = new QGraphicsEllipseItem( 0, &scene );
item2->setRect( -50.0, -50.0, 50, 100.0 );
scene->addItem(item2);
view->show();
return app.exec();
}
代码2:(这是简化版,但已损坏)
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QImage myImage;
myImage.load("2.png");
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsView* view = new QGraphicsView(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(myImage));
scene->addItem(item);
view->show();
return app.exec();
}