Getting image dimensions



  • Hello again,
    I want to be able to get image dimensions from an image that is loaded into a scene (see code bellow)
    I'm not sure how to do so, seeing that the only thing I found for only seemed to be for QImage, but maybe I'm misguided. I want a label called label to display the dimensions of the image (ui->label->setText) *note only one image is loaded at a time

    Also, the label will only display the image dimensions after a button is pressed that I haven't even added in yet. (Don't worry about that part i will add it)

    Header file

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QFileDialog>
    #include <QtGui>
    #include <string>
    #include <limits>
    #include <vector>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
       Q_OBJECT
    
    public:
       explicit MainWindow(QWidget *parent = 0);
       ~MainWindow();
    
    private slots:
       void on_load_clicked();
    
    private:
       Ui::MainWindow *ui;
       QPixmap image;
       QImage  *imageObject;
       QGraphicsScene *scene;
    };
    
    #endif // MAINWINDOW_H
    
    

    source file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <iostream>
    #include <limits>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    #include <QFileDialog>
    #include <QtGui>
    #include <QPixmap>
    #include <QGraphicsScene>
    //#include <QtAlgorithms>
    
    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);
    }
    

  • Lifetime Qt Champion

    Hi,

    Do you mean show the QImage::size value ?

    By the way, why re-allocate a new QImage each time ? And why do it on the heap. Usually the stack is enough for that class.



  • @SGaist

    Yes that's what I mean. I guess my question is just how to use and declare it. I have a hard time following documentation and only benefit from seeing an example code myself.

    And what do you mean?


  • Lifetime Qt Champion

    Something like:

    ui->label->setText(QString("%1x%2").arg(image.width(), image.height());
    


  • @SGaist
    Hi,
    Text Label does not return the image width and height. Am I missing something? Sorry for all the confusion, very new to Qt
    Is %1x%2 just a placeholder? Edit: Stupid me that's the formatting
    Btw, you forgot a ) before ;

    Guessing I need to add something?



  • @CookieMaster

    I think the formatting is incorrect.



  • @CookieMaster

    QSize size = image.size();
            ui->label->setText(QString("%1x%2").arg(size.width()).arg(size.height()));
    

    Thanks @SGaist I now know what your example meant, it helped a lot. (I even used it)



  • @SGaist said in Getting image dimensions:

    By the way, why re-allocate a new QImage each time ? And why do it on the heap. Usually the stack is enough for that class.

    +1. I have seen this kind of codes many times, usually they do not care as long as their code "works".


Log in to reply