PDA

View Full Version : diplaying image



peace_comp
1st April 2008, 15:23
Hey..
I m new begginer in Qt .. I had a problem to display a simple image on a graphicsView ..
I tried the following instruction :



QPixmap image(path);
myview->setBackgroundBrush(image); //myview is a graphicsView Class

But Nothing Appear .. Actually I had a trouble with Using GraphicsView .. there is many class (GraphicsScene .. GrphicItem ... ) But I really Dont Know Wht To USe ..

Plzzz If Some Could Help Me On ThaT!!!!!!!!

aamer4yu
1st April 2008, 16:08
do u want to display image on graphics view only ??
U can use QLabel too. Check the image viewer example in Qt Demo

jpn
1st April 2008, 17:00
But I really Dont Know Wht To USe ..

Plzzz If Some Could Help Me On ThaT!!!!!!!!
I'm afraid this won't encourage people to help you...

Anyway, what does


#include <QtDebug>
#include <QImageReader>

// after constructing the image
qDebug() << path << image.isNull() << QImageReader::supportedFormats();

output?

peace_comp
2nd April 2008, 10:53
qDebug() << path << image.isNull() << QImageReader::supportedFormats();

Error: supportedFormats() is not a memeber of QImageReader...

well .. actualy I read Some Tuto About Graphics ...What I understand is That a QGraphicsScene sereves as a container for QgraphicsItem ..and The QGraphicsView Provides the View Widget that Visualize the contain of scene..
So I tryed That Code :


//connect (actionOpen,SIGNAL(triggered()),this,SLOT(Open())) ;

void myform::Open(){

path=QFileDialog::getOpenFileName(this,"Choose a file to oepn",QString::null,QString::null);

if(!path.isEmpty()){
QPixmap image(path);
QGraphicsPixmapItem mypixmap(image);
scene->addItem(&mypixmap); //scene declared as QGraphicsScene
graphicsView->setScene(scene); //graphicsView as QGraphicsView
graphicsView->show();
}
}
but Nothing was displayed on the graphicsView ... (but a scrollbar was added to the View)
Sould I refresh My graphicsView or did I miss some function for displayin;??

Thanks for Help

jpn
2nd April 2008, 12:22
qDebug() << path << image.isNull() << QImageReader::supportedFormats();

Error: supportedFormats() is not a memeber of QImageReader...
Sorry for the typo. It should be "supportedImageFormats". But you know, you could just have looked up in QImageReader docs what's the correct name of the function... Takes less time than posting here. :)

Anyway, the problem seems to be that you allocate the item on the stack. It goes out of scope and gets automatically destructed according to normal C++ rules. You should allocate it on the heap instead of you want it to remain alive.