The program freezes when an instance of a class is created
-
Hi,
I have the following code to display an image:void MainWindow::on_tableView_clicked(const QModelIndex &index) { int row = index.row (); int column = index.column (); qDebug() << "Index: " << "(" << row <<"," << column << ")"; if(column == 2) { qDebug() << "Column 2 was chosen."; QPixmap imageToEnlarge = index.data(Qt::DecorationRole).value<QPixmap>(); ImageDisplay *mImageDisplay = new ImageDisplay(this); mImageDisplay->viewImage(imageToEnlarge); } }
The called function:
void ImageDisplay::viewImage(QPixmap imageToEnlarge) { int w = ui->ImageLabel->width (); int h = ui->ImageLabel->height (); ui->ImageLabel->setPixmap (imageToEnlarge.scaled(w,h,Qt::KeepAspectRatio)); }
When program execution reaches ImageDisplay * freezes and crashes the program. What did I do incorrectly? Thank you for your help.
-
What does the
ImageDisplay
constructor look like? -
#ifndef IMAGEDISPLAY_H #define IMAGEDISPLAY_H #include <mainwindow.h> #include <additem.h> #include <review.h> #include <QDialog> namespace Ui { class ImageDisplay; } class ImageDisplay : public QDialog { Q_OBJECT public: explicit ImageDisplay(QWidget *parent = 0); ~ImageDisplay(); void viewImage(QPixmap imageToEnlarge ); private: Ui::ImageDisplay *ui; }; #endif // IMAGEDISPLAY_H
-
It also displays the following messages:
can't find linker symbol for virtual table forQPixmap' value found
_q_ObjectMutexPool' instead
can't find linker symbol for virtual table forQPixmap' value found
_q_ObjectMutexPool' instead
Column 2 was chosen.
can't find linker symbol for virtual table forQPixmap' value found
_q_ObjectMutexPool' instead
can't find linker symbol for virtual table forQPixmap' value found
_q_ObjectMutexPool' instead -
@jsulm
Here it is:#include "imagedisplay.h" #include "ui_imagedisplay.h" ImageDisplay::ImageDisplay(QWidget *parent) : QDialog(parent), ui(new Ui::ImageDisplay) { ui->setupUi(this); ImageDisplay(imageToEnlarge); } ImageDisplay::~ImageDisplay() { delete ui; } void ImageDisplay::viewImage(QPixmap imageToEnlarge) { int w = ui->ImageLabel->width (); int h = ui->ImageLabel->height (); ui->ImageLabel->setPixmap (imageToEnlarge.scaled(w,h,Qt::KeepAspectRatio)); }
Thank you.
-
ImageDisplay::ImageDisplay(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImageDisplay)
{
ui->setupUi(this);
ImageDisplay(imageToEnlarge); <<<<<< Call ctor again !! ?!should that not be viewImage(QPixmap imageToEnlarge) ?
-
@kshegunov said in The program freezes when an instance of a class is created:
How does this even compile?
another problem is that in the constructor
imageToEnlarge
should be an undefined symbol. if it is defined in that constructor then there is a naming conflict in viewImage. this should 100% not compile -
ImageDisplay::ImageDisplay(QWidget *parent) : QDialog(parent), ui(new Ui::ImageDisplay) { ui->setupUi(this); ImageDisplay(imageToEnlarge); } ImageDisplay::~ImageDisplay()
I deleted ImageDisplay(imageToEnlarge). As a result it doesn't freeze but doesn't display the image I clicked on either.
-
As what others suggested, the content of the form constructor is wrong.
Your widgets are already taken care of by setupUI().
Thus, the logic of displaying the image should just be in viewImage ().QPixmap imageToEnlarge = index.data(Qt::DecorationRole).value<QPixmap>();
where is the data in this?
here's a brief example using QT's QImage.
QPixmap scaledPixmap = QPixmap::fromImage (image).scaled ( QSize (w, h), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); ui->ImageLabel->setPixmap (scaledPixmap);
-
The following seems to work:
void MainWindow::on_tableView_clicked(const QModelIndex &index) { int row = index.row (); int column = index.column (); qDebug() << "Index: " << "(" << row <<"," << column << ")"; if(column == 2) { qDebug() << "Column 2 was chosen."; QPixmap imageToEnlarge = index.data(Qt::DecorationRole).value<QPixmap>(); int w = ui->label_Temp->width(); int h = ui->label_Temp->height (); ui->label_Temp->setPixmap (imageToEnlarge.scaled(w,h,Qt::KeepAspectRatio)); ImageDisplay *mImageDisplay = new ImageDisplay; mImageDisplay->viewImage(imageToEnlarge); mImageDisplay->exec (); } }
Thank you for your help.
-
super
You should get rid of new or add a deleteImageDisplay *mImageDisplay = new ImageDisplay; mImageDisplay->viewImage(imageToEnlarge); mImageDisplay->exec (); delete ImageDisplay; /// clean up
or simply dont use new as you use exec()
ImageDisplay mImageDisplay; mImageDisplay.viewImage(imageToEnlarge); mImageDisplay.exec ();