PDA

View Full Version : Display an image in a QGraphicsview



emilio
3rd March 2011, 22:45
Hello,

I'm developing an application that displays a default image in a QGraphicsiew (named viewer) when it's executed and displays other image when a button it's clicked but my code doesn't work:

main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
catiz w;
w.showFullScreen();

return a.exec();
}

catiz.cpp


...
catiz::catiz(QWidget *parent) : QWidget(parent),ui(new Ui::catiz)
{
ui->setupUi(this);

QGraphicsScene scene;
QPixmap pixmap(":/init.jpg");
scene.addPixmap(pixmap);
ui->viewer->setScene(&scene);
}
...

What could be wrong? I'm using QtCreator.

Best regards.

cooper
3rd March 2011, 23:01
Hi emilio,

try to use a graphic view widget on your frame. and add the following code in your app:


...
catiz::catiz(QWidget *parent) : QWidget(parent),ui(new Ui::catiz)
{
ui->setupUi(this);

QGraphicsScene scene;
QPixmap pixmap(":/init.jpg");
scene.addPixmap(pixmap);
//ui->viewer->setScene(&scene);

// add here
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
}
...


hope it works.

emilio
3rd March 2011, 23:16
Hello cooper,


ui->viewer it's a QGraphicsview widget, thanks anyway.

cooper
4th March 2011, 00:27
does it work after you add:


ui->viewer->show();

emilio
4th March 2011, 08:13
I tried but it still doesn't work.

sibercekirge
25th March 2011, 08:11
Hello, I am new to Qt an C++. I had the same problem. After looking for the forum I managed to display an image on an graphicsView which is on QWidget user form
I created a new project with QtGui App. put a push button and a graphicsView widget. when I push the button. it creates a scene object, a pixmap and attach it to the viewer
Below is the code

#include <QtGui>
#include "resimoku.h"
#include "ui_resimoku.h"

ResimOku::ResimOku(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResimOku)
{
ui->setupUi(this);

}

ResimOku::~ResimOku()
{
delete ui;
}

void ResimOku::on_pushButton_clicked()
{
QGraphicsScene *scene = new QGraphicsScene;
QPixmap pixmap("C:/resim.jpg");
scene->addPixmap(pixmap);
ui->view->setScene(scene);
ui->view->show();
}

The decleration of the scene object is solved the problem I think. It is a matter of C++ knowledge which i have a little.
Thanks for forum users.

jano_alex_es
25th March 2011, 10:48
Try to create the QGraphicsScene dinamically... because static variables are deleted when you are out of the scope. If it's dinamic going out of the scope only deletes the pointer.

sibercekirge
26th March 2011, 08:02
I made a change to scene object decleration in order to use it out of pushbuttons scope. I declare it as a private member of ResimOku object. create it in ResimOku decleration and use it in pushbutton events. With this the scope of scene object is ResimOku object scope.
Header file

#ifndef RESIMOKU_H
#define RESIMOKU_H
#include <QtGui>
#include <QMainWindow>

namespace Ui {
class ResimOku;
}

class ResimOku : public QMainWindow
{
Q_OBJECT

public:
explicit ResimOku(QWidget *parent = 0);

~ResimOku();

private:
Ui::ResimOku *ui;
QGraphicsScene *scene;
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};

#endif // RESIMOKU_H

CPP FILE


#include "resimoku.h"
#include "ui_resimoku.h"


ResimOku::ResimOku(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResimOku)
{
ui->setupUi(this);
scene = new QGraphicsScene;
}

ResimOku::~ResimOku()
{
delete ui;
}

void ResimOku::on_pushButton_clicked()
{
QPixmap pixmap("C:/resim.jpg");
scene->addPixmap(pixmap);
ui->view->setScene(scene);
ui->view->show();
}

void ResimOku::on_pushButton_2_clicked()
{
QPixmap pixmap("C:/resim2.jpg");
scene->addPixmap(pixmap);
ui->view->setScene(scene);
ui->view->show();
}

Harry443
6th December 2011, 06:55
Hello QtCentre......

How can we display the image on GraphicsView in widget.ui using the above code through pushbutton signals???
I had used QWidget and unable to succeed in doing so.
plzz help me in achieving the same