PDA

View Full Version : my QGraphicView problem



irmakci
18th July 2008, 12:59
:confused:
Hi,
I used the code below normally in QApplication


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pix;
QGraphicsView* gr;
//QImage image;
QString currentFile = QFileDialog::getOpenFileName(0,"Open File", QDir::currentPath());
QImage image(currentFile);
QGraphicsScene scene = new QGraphicsScene();
//pix.load(":/Resources/open.png");
//pix.load(currentFile);

scene.addPixmap(QPixmap::fromImage(image));
gr= new QGraphicsView();
gr->setScene(&scene);
//setCentralWidget(gr);
gr->show();

return a.exec();
}

and this works. I can see the image loaded.

But when I used the same code in QMainWindow
such as


deneme::deneme(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QPixmap pix;
QGraphicsView* gr;
//QImage image;
QString currentFile = QFileDialog::getOpenFileName(0,"Open File", QDir::currentPath());
QImage image(currentFile);
QGraphicsScene scene = new QGraphicsScene();

scene.addPixmap(QPixmap::fromImage(image));
gr= new QGraphicsView();
//gr->setBackgroundBrush(QImage(currentFile));
//gr->setForegroundBrush(QImage(currentFile));
gr->setScene(&scene);
//setCentralWidget(gr);
gr->show();

}
this does not work. And I can only see white graphics widget when I loaded the image.

How can I solve this problem. Thanks in advance
regards

aamer4yu
18th July 2008, 13:57
try this -

deneme::deneme(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QPixmap pix;
QGraphicsView* gr;
QString currentFile = QFileDialog::getOpenFileName(0,"Open File", QDir::currentPath());
QImage image(currentFile);
QGraphicsScene *scene = new QGraphicsScene();

scene->addPixmap(QPixmap::fromImage(image));
gr= new QGraphicsView();
gr->setScene(scene);
setCentralWidget(gr);
}

Does this work for you ?? You are creating scene as local object which will be destroyed after the constructor code .

irmakci
19th July 2008, 12:36
Yes, when scene was made as private variable in class, then it worked. Thanks. But Why is it destroyed I can't understand? Because graphics view widget is not destroyed. And When I also made this simple code in button click, same thing did not work although it is not in the constructor.
Anyway It must be global (w.r.t. class) variable in the class.

caduel
19th July 2008, 18:40
If you declare a variable in a method, it will be destroyed when it goes out of scope (i.e. when the method is left).

Your scene is local to the constructor's body. It is destroyed after the constructor is completed!