Crop Function showing no result



  • Hi everyone.
    I wrote a function to crop an image. The function is as follows:
    void ImageViewer::crop()
    { QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),QDir::currentPath());
    QImage image(fileName);
    QImage copy ;
    copy = image.copy( 0, 0, 128, 128);
    }
    The program runs without error. But when I try to crop an image, it shows no change and just prompts me to save it, that's all...no change.
    Can anyone tell me where the problem is
    Thanks


  • Moderators

    Hi,

    Please code tags to display the code. This way it will be easier for others to understand your code.

    From your code what i understand is,
    @QString fileName=QFileDialog::getSaveFileName(this,tr(“Save File”),QDir::currentPath());@
    First you are getting the filename that is to be saved and then passing it to the QImage using,
    @ QImage image(fileName);@

    But Where is your actual file which is to be cropped ?



  • Sorry, my mistake, copied the wrong code snippet there...:P
    So can you tell me how to modify the code suitably?


  • Moderators

    Something like this.
    @
    QImage img("pathtoyourimagefile");
    QImage copy = img.copy(0,0,128,128);@

    Thus "copy" contains the cropped image.



  • but the image file name was input by the user. How should I code for that?


  • Moderators

    Thus when you have done cropping the file ask for user to enter its name.
    So your this code,
    @QString fileName=QFileDialog::getSaveFileName(this,tr(“Save File”),QDir::currentPath());@
    gets the name of the user entered file.
    Then just save the image with that name.
    so,
    @
    QImage img("pathtoyourimagefile");
    QImage copy = img.copy(0,0,128,128);
    copy.save(fileName,"jpg");
    @


Log in to reply
 

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