January 19, 2012

grizz grizz
Lab Rat
6 posts

GraphicsView doesn’t display Pixmap

 

Hi, I wrote program which has to display an image. I used Pixmap, GraphicsScene and GraphicsView, but it doesn’t work… Could you tell me, what’s wrong?
QtCreator 2.4.0

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9.     QPixmap *obraz = new QPixmap("C:\chrome.png");
  10.     QGraphicsScene *scena = new QGraphicsScene(this);
  11.     scena->addPixmap(*obraz);
  12.     QGraphicsView *widok = new QGraphicsView(this);
  13.     widok->setScene(scena);
  14.     widok->setGeometry(50,50,270,270);
  15. }
  16.  
  17. MainWindow::~MainWindow()
  18. {
  19.     delete ui;
  20. }

8 replies

January 19, 2012

Jake007 Jake007
Lab Rat
231 posts

Do you even display your widgets in MainWindow?

If you placed it graphically, try calling it from namespace ui.

Regards,
Jake

P.S.: Are you maybe from Slovenia?

 Signature 

——————————————-
Code is poetry

January 19, 2012

grizz grizz
Lab Rat
6 posts

This is my main.cpp:

  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     MainWindow w;
  8.     w.show();
  9.    
  10.     return a.exec();
  11. }

When I add text to scene, GraphicsView displays it.

No, I’m not from Slovenia. Greetings from Poland,

January 19, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2068 posts

You’ve forgotten to set your QGraphicsView as the central widget.

  1. MainWindow::MainWindow(QWidget *parent) : ...
  2. {
  3.     ...
  4.     setCentralWidget(widok);
  5. }

January 19, 2012

grizz grizz
Lab Rat
6 posts

It doesnt’t help. Of course I included <QGraphicsView>.

January 19, 2012

Jake007 Jake007
Lab Rat
231 posts

Declare your QGraphicsView in your class header file.
Like this:

  1. class MainWindow : public QMainWindow
  2. {
  3.   Q_OBJECT
  4.  
  5.  public:
  6.   explicit MainWindow(QWidget *parent = 0);
  7.   ~MainWindow();
  8.  
  9.   QGraphicsView *view;
  10.   QGraphicsScene *scene;
  11.  
  12.  private:
  13.   Ui::MainWindow *ui;
  14. };

And your .cpp

  1. MainWindow::MainWindow(QWidget *parent) :  QMainWindow(parent),  ui(new Ui::MainWindow)
  2. {
  3.  ui->setupUi(this);
  4.  
  5.  view = new QGraphicsView;
  6.  scene = new QGraphicsScene;
  7.  
  8.  view->setScene(scene);
  9.  
  10.  this->setCentralWidget(view);
  11. }

Similar words.
Easily replaced ;).

 Signature 

——————————————-
Code is poetry

January 19, 2012

grizz grizz
Lab Rat
6 posts

Still doesn’t work :/.

January 19, 2012

Jake007 Jake007
Lab Rat
231 posts

Your image path is also incorrect.
If you are using backslash, you should put \\, or you can also put one slash:
“C:/chrome.png”

  1.  ui->setupUi(this);
  2.  
  3.  view = new QGraphicsView;
  4.  scene = new QGraphicsScene;
  5.  QPixmap *pix = new QPixmap("C:/pix.png");
  6.  scene->addPixmap(*pix);
  7.  view->setScene(scene);
  8.  
  9.  this->setCentralWidget(view);

 Signature 

——————————————-
Code is poetry

January 19, 2012

grizz grizz
Lab Rat
6 posts

Thank u :)

 
  ‹‹ Delegate display on row selection in QTableView      problem in deleting data from the database? ››

You must log in to post a reply. Not a member yet? Register here!