I work on Qt GUI C++ project. I try to display image from resource files, and there are more requirements like playing slideshow photos. I meet difficult when debugging, it just showed white screen. I thought the image was not loaded to GraphicsView. In in main.cpp and mainwindow.ui, I keep them like default, no editing. So please help me to fix this problem. Thanks in advance
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <QMessageBox>
#include <QGraphicsView>
#include <QStackedWidget>
#include <QGraphicsScene>
#include <QDataStream>
#include <QFile>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap file(":/rose.jpg");
QStackedWidget *temp = new QStackedWidget();
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem img(file);
scene.addItem(&img);
temp->addWidget(&view);
setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
Your QGraphicsView
and QGraphicsScene
instance are local variable, they will be destroyed when out of the MainWindow
constructor scope.
please try it like this.
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *view = new QGraphicsView(scene);
If it still does't work, please check your QPixmap
file is null or not just like what @Wagmare say.