Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The program freezes when an instance of a class is created

The program freezes when an instance of a class is created

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 9 Posters 5.1k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jsulmJ jsulm

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

    G Offline
    G Offline
    gabor53
    wrote on last edited by
    #9

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

    jsulmJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #10

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

      kshegunovK 1 Reply Last reply
      2
      • G gabor53

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #11

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

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • mrjjM mrjj

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #12

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

          Read and abide by the Qt Code of Conduct

          VRoninV 1 Reply Last reply
          1
          • kshegunovK kshegunov

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #13

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • G Offline
              G Offline
              gabor53
              wrote on last edited by
              #14
              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.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                josephdp
                wrote on last edited by
                #15

                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);
                
                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #16

                  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.

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #17

                    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 ();
                    
                    1 Reply Last reply
                    2
                    • G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #18

                      Thank you!!

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved