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@
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap *obraz = new QPixmap("C:\chrome.png");
QGraphicsScene *scena = new QGraphicsScene(this);
scena->addPixmap(*obraz);
QGraphicsView *widok = new QGraphicsView(this);
widok->setScene(scena);
widok->setGeometry(50,50,270,270);
}MainWindow::~MainWindow()
{
delete ui;
}
@ -
This is my main.cpp:
@
#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
@When I add text to scene, GraphicsView displays it.
No, I'm not from Slovenia. Greetings from Poland,
-
Declare your QGraphicsView in your class header file.
Like this:@class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();QGraphicsView *view;
QGraphicsScene *scene;private:
Ui::MainWindow *ui;
};@And your .cpp
@MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);view = new QGraphicsView;
scene = new QGraphicsScene;view->setScene(scene);
this->setCentralWidget(view);
}@Similar words.
Easily replaced ;). -
Your image path is also incorrect.
If you are using backslash, you should put \, or you can also put one slash:
"C:/chrome.png"@ ui->setupUi(this);
view = new QGraphicsView;
scene = new QGraphicsScene;
QPixmap *pix = new QPixmap("C:/pix.png");
scene->addPixmap(*pix);
view->setScene(scene);this->setCentralWidget(view);
@