Getting image path



  • Hello , I know that I could get the image path if I included something in the private part of my class. However, I don't want to do that because then when the project is open and will put the file dialog without a button being pressed. You will see what I mean

    Header file:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QFileDialog>
    #include <iostream>
    #include <vector>
    #include <string>
    #include <QFileDialog>
    #include <QtGui>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QSize>
    
    // Qt makes a class already when creating a project, just put everying in here...
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
       Q_OBJECT
    
    public:
       explicit MainWindow(QWidget *parent = 0);
       ~MainWindow();
    
    private slots:
       void on_load_clicked();
       void on_close_clicked();
       void on_view_clicked();
    
    private:
       Ui::MainWindow *ui;
       QPixmap image;
       QImage  *imageObject;
       QGraphicsScene *scene;
    };
    
    #endif // MAINWINDOW_H
    
    

    source file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_load_clicked()
    
    {
        QString imagePath = QFileDialog::getOpenFileName();
        imageObject = new QImage();
        imageObject->load(imagePath);
        image = QPixmap::fromImage(*imageObject);
        scene = new QGraphicsScene(this);
        scene->addPixmap(image);
        scene->setSceneRect(image.rect());
        ui->graphicsView->setScene(scene);
    }
    
    void MainWindow::on_close_clicked()
    {
        scene->clear();
        ui->graphicsView->items().clear();
    }
    
    void MainWindow::on_view_clicked()
    {
        QSize size = image.size();
        ui->size->setText(QString("Size: %1x%2").arg(size.width()).arg(size.height()));
        //ui->path->setText(QString(imagePath));
    }
    
    

    You can see i commented out the last thing in the source file because it can not retrieve the image path without that being in the header file, but if i did that then when the program starts it will prompt the user to pick a file, witch i don't want to happen unless the button is pressed.

    Is their a function that gets image path or can I somehow get around this. Thanks!!



  • Just an additional question, is it possible to get the color depth (bits per pixel)
    and get the file date?



  • 1 : your codes got memory leak

    imageObject = new QImage(); //you can new it without ()
    imageObject->load(imagePath);
    

    you can save your headache by putting imageObject on stack

    imageObject.load(imagePath);
    

    If you have to use pointer(this should be rare case in c++ if you use it right, the ui pointer generated by Qt Designer is an exception), there are two ways to make your life easier to deal with

    a : protect your resource by smart pointer, in most of the cases, std::unique_ptr is the best choice, as cheap as raw pointer and easier to "see" the object life time
    b : use parent and child relationship to handle your resources, in many cases, Qt codes favor parent and child relationship to protect the resources more than smart pointer

    Why should C++ programmers minimize use of 'new'?.

    2 : you may avoid one memory copy by &&

    image = QPixmap::fromImage(std::move(imageObject));
    

    Or you could just load the image by QPixmap rather than load it by QImage.Make sure you know the difference of QPixmap and QImage before you pick them

    3 : @CookieMaster said in Getting image path:

    You can see i commented out the last thing in the source file because it can not retrieve the image path without that being in the header file, but if i did that then when the program starts it will prompt the user to pick a file, witch i don't want to happen unless the button is pressed.

    I beg of your pardon, but I do not know what do you want to do

    4 : @CookieMaster said in Getting image path:

    Just an additional question, is it possible to get the color depth (bits per pixel)
    and get the file date?

    you can use QImageReader to help you find out the format of the image.

    QImageReader im_reader("myim.jpg");
    auto const im_format = im_reader.imageFormat(); //You should be able to determine how many bits per pixel by the format
    

    Use QFileInfo to get the lastModified/lastRead info

    QFileInfo file("myim.jpg");
    auto const last_modified_date = file.lastModified();
    auto const last_read_date = file.lastRead();
    

  • Moderators

    @CookieMaster Sorry, your description is really unclear. The imagePath is set in on_load_clicked(), before it is called there is no path - why not just set empty string in on_view_clicked() then if it is called before on_load_clicked()? You can disable the button which triggers on_view_clicked() and enable it in on_load_clicked() if an image was loaded.
    "because it can not retrieve the image path without that being in the header file" - what does this mean?



  • @jsulm
    Hi, sorry

    I ended up figuring it all out.
    Also I apologize for it being unclear.

    @tham Thank you, ended up getting color depth, and getting the name and such...

    fixed that memory leak as well.


Log in to reply