I want to display an image in a “functional” programming style. Basically, I operate a number of processes on my image and I sometimes want to display the result. So I am trying:
int display(string file, int argc, char *argv[])
{
QApplication a1(argc,argv);
QImage myImage;
myImage.load(file.c_str());
QLabel myLabel;
myLabel.setPixmap(QPixmap::fromImage(myImage));
myLabel.show();
return a1.exec();
}
int main(int argc, char *argv[])
{
MyImageDataStructure img;
img.erosion(5);//process 1
img.save("lenaero.png");
display("lenaeor.png",argc,argv); // display the first result
img.dilation(5);//process 2
img.save("lenaopening.png");
display("lenaopening.png",argc,argv); // display the second result
return 1;
}
But at the second execution of the display function, an error occurs. Do you have any ideas to solve this problem while keeping this logic?
Thanks
Note: I don’t want to include external libraries except Qt and I understand that I want to work outside the Qt logic.
I don’t really understand what you mean by “display an image in a
functionalstyle”. I think what you mean is that you want a function to do the display part for an image. In that case, there seems to be a misconception on your part as to what a “functional” paradigm actually is.Nevertheless, your problem is very different. You need to move the QApplication declaration into the main function:
And move it out of the
display()function.EDIT
I re-read your code, and there are a lot of things wrong with it. You SHOULD NOT be creating a separate application every time you wish to display an image. A much better thing would be to use a
QDialog. Create a dialog, with a label as an image and callshowon it every time you wish to display the image.Useful links:
http://qt-project.org/doc/qt-4.8/QDialog.html
http://sector.ynet.sk/qt4-tutorial/my-first-qt-gui-application.html (Nice overview of creating and showing dialogs)