The program freezes when an instance of a class is created
-
@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 ();