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.



  • Hi, maybe the QPixmap wasn't set correctly in your model's data() function? You could check if imageToEnlarge is null, then don't proceed.



  • I'd try to pass imageToEnlarge argument by reference instead of value. Maybe something bad is going on when copying it during the call.



  • What does the ImageDisplay constructor look like?



  • @Paul-Colby

    #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
    
    


  • Now it also sends a SIGSEGV error.



  • It also displays the following messages:
    can't find linker symbol for virtual table for QPixmap' value found_q_ObjectMutexPool' instead
    can't find linker symbol for virtual table for QPixmap' value found_q_ObjectMutexPool' instead
    Column 2 was chosen.
    can't find linker symbol for virtual table for QPixmap' value found_q_ObjectMutexPool' instead
    can't find linker symbol for virtual table for QPixmap' value found_q_ObjectMutexPool' instead


  • Moderators

    @gabor53 Can you please show the content of the ImageDisplay constructor, not just its declaration?



  • @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.


  • Qt Champions 2016

    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) ?


  • Moderators

    @gabor53 To add to @mrjj: what is imageToEnlarge in

    ImageDisplay::ImageDisplay(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::ImageDisplay)
    {
        ui->setupUi(this);
    
        ImageDisplay(imageToEnlarge);
    }
    

  • Qt Champions 2016

    How does this even compile? You're not allowed to call constructors explicitly ... probably it creates a temporary ...



  • @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.


  • Qt Champions 2016

    super
    You should get rid of new or add a delete

    ImageDisplay *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 ();
    


  • Thank you!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.