I'm writing a simple Qt c++ program to compare two images. I have already asked a question on the project's design (Qt Model-View-Controller) but my professor said i didn't have to use any of qt predefined model/views.
However now i'm having an issue in changing the way the images are displayed. More precisely, after i have already loaded two images, cropped them and displayed them flawlessly, i get a segmentation fault when trying to re-display the images cropped in a different way. Here's some code:
File MainWindow.h:
class MainWindow : public QMainWindow, public View, public Controller
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
virtual void myUpdate();
private slots:
void open();
void loadImage();
void zoomIn();
void zoomOut();
void normalSize();
void fitToWindow();
void about();
void vertical();
void horizontal();
private:
Ui::MainWindow *ui;
Image imageComposition;
QImage *result;
QImage* displayVertical(QImage *res, QPainter *painter);
QImage* displayHorizontal(QImage *res, QPainter *painter);
void createActions();
void createMenus();
void updateActions();
void scaleImage(double factor);
void diffImages();
.... // methods and attributes non relevant for this question.
File MainWindow.cpp:
....//methods non relevant for this question.
void MainWindow::diffImages()
{
// **SIGEGV happening on the following line!!**
result = new QImage(QSize(imageComposition.getLeft()->width(),imageComposition.getLeft()->height()), QImage::Format_ARGB32_Premultiplied);
ui->Canvas->resize(imageComposition.getLeft()->width(),imageComposition.getLeft()->height());
QPainter *painter = new QPainter(result);
switch(imageComposition.getDivisionBar()) {
case 0 :
result = displayVertical(result, painter);
break;
case 1 :
result = displayHorizontal(result, painter);
break;
};
ui->Canvas->setPixmap(QPixmap::fromImage(*result));
}
.....
QImage* MainWindow::displayVertical(QImage *res, QPainter *painter)
{
QRect leftRect(QPoint(0,0), QPoint(imageComposition.getLeft()->width()/2,imageComposition.getLeft()->height() - 1 ));
QImage leftSide(imageComposition.getLeft()->copy(leftRect));
QRect rightRect(QPoint(imageComposition.getRight()->width()/2, 0), QPoint(imageComposition.getRight()->width() - 1,imageComposition.getRight()->height() - 1));
QImage rightSide(imageComposition.getRight()->copy(rightRect));
painter->drawImage(0,0, leftSide);
painter->drawImage(res->width()/2, 0, rightSide);
return res;
}
QImage *MainWindow::displayHorizontal(QImage *res, QPainter *painter) {
QRect upperRect(QPoint(0,0), QPoint(imageComposition.getLeft()->width() - 1, imageComposition.getLeft()->height() / 2));
QImage upperSide(imageComposition.getLeft()->copy(upperRect));
QRect lowerRect(QPoint(0,imageComposition.getRight()->height() / 2), QPoint(imageComposition.getRight()->width() - 1, imageComposition.getRight()->height() - 1));
QImage lowerSide(imageComposition.getRight()->copy(lowerRect));
painter->drawImage(0, 0, upperSide);
painter->drawImage(0, res->height() / 2, lowerSide);
return res;
}
So the segmentation fault happens in diffImages(), at the first line, when result (a QImage already containing something, as it was initialized the first time two images are rendered.) should be re-initialized to display the images in another way. That is when the user press the button "Display horizontally".
I searched the web and some suggest to put the painter->drawImage(), wich is contained in the diffImages() function, in an overloaded implementation of MainWindow::paintEvent(QPaintEvent *). But that way diffImages() is called whenever something needs to be redrawed, including the initial window displaying, wich leads to a loop of redraw() calls. So I decided not to follow that suggestion.
From what i can understand, the possible cause of the problem could be the fact that result is still being displayed on the screen inside the QLabel when the method called after wants to re-initialize it. What do you suggest?? Thank you in advance.
p.s. I tried to be as clear as possible, but if you need the full source or other explanation please let me know.