0

I've created a Graphics View object in my mainwindow.ui file and I'm trying to display an image in that. For TextBrowser objects, I was doing like this

QTextBrowser *textBrowser_Actors = this->findChild<QTextBrowser*>("textBrowser_Actors");
textBrowser_Actors->setText(QString::fromUtf8(movie.get_actors().c_str()));

Similar way, how do I set an image after finding a GraphicsView by the below method?

QGraphicsView* movie_poster = this->findChild<QGraphicsView*>("movie_poster");

I tried the following from googling a bit, but couldn't get it working so far.

QGraphicsScene* scene = new QGraphicsScene();
movie_poster->setScene(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage("movie.jpg"));
scene->addItem(item);
movie_poster->show();

Edit-1

int main(int argc, char *argv[])
{
    std::vector<Movie> movie_vector; // This is where movie DB will be read to, and new movies will be added to
    MainWindow w;
    w.setWindow(movie_vector[0]); // calling setWindow with first movie
    w.show();
    return a.exec();
}

//setWindow definition    
void MainWindow::setWindow(Movie &movie) {

// Next two lines gets the textBrowser object and set its value to movie title
    QTextBrowser *textBrowser_Title = this->findChild<QTextBrowser*>("textBrowser_Title");
    textBrowser_Title->setText(QString::fromUtf8(movie.get_title().c_str()));

// This is where I'm trying to get GraphicsView object and set an image in it.
    QGraphicsView* movie_poster = this->findChild<QGraphicsView*>("movie_poster");
    QGraphicsScene* scene = new QGraphicsScene();
    movie_poster->setScene(scene);
    QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap("C:\Users\Name\Desktop\codes\Qt\MovieDB\titanic.jpg"));
    scene->addItem(item);
    movie_poster->show();
}
akhilc
  • 181
  • 4
  • 19
  • 1
    change `QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage("movie.jpg"));` to `QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap("movie.jpg")); ` – eyllanesc Apr 06 '18 at 07:10
  • Replace "movie.jpg" with the fullpath: "/path/of/movie.jpg", or use a qresource to store the image or place movie.jpg next to the executable. – eyllanesc Apr 06 '18 at 07:12
  • It's still not working, but no errors though. The GraphicsView widget box just shows up blank. This is the code after your suggestion. QGraphicsView* movie_poster = this->findChild("movie_poster"); QGraphicsScene* scene = new QGraphicsScene(); movie_poster->setScene(scene); QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap("C:\Users\Name\Desktop\codes\Qt\MovieDB\movie.jpg")); scene->addItem(item); movie_poster->show(); – akhilc Apr 06 '18 at 08:09
  • provide a [mcve] – eyllanesc Apr 06 '18 at 08:10
  • @eyllanesc I've added my code in Edit-1 in main question. – akhilc Apr 06 '18 at 08:21
  • Hey, it's working, thanks. Actually I had to use forward slashes(`/`) instead of backslashes(`\\`) in the image location. I copied from windows explorer where it was using backslashes. – akhilc Apr 06 '18 at 08:53
  • @akhilc Please post the corrected code with explanation as answer yourself and mark it as accepted. – Kathir Oct 31 '19 at 11:57

1 Answers1

0

Maybe this answer help you: https://stackoverflow.com/a/7138147/6631835

this is my code:

#include "mainwindow.h"

#include <QGraphicsView>
#include <QPixmap>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
     resize(800,800);
    this->image=new QImage();
     openImage();
}
void MainWindow::openImage(){
    image->load("/home/ztftrue/Downloads/test.jpg");
    QGraphicsScene* scene=new QGraphicsScene() ;
    QGraphicsView* view = new QGraphicsView(scene);
    QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));
    scene->addItem(item);
    view->show();
 }
MainWindow::~MainWindow()
{

}
tf z
  • 21
  • 1