How to display a picture
-
Hello guys,
I'm a beginner in Qt and i want to display a picture in Qt and i want to do whatever i want with ( with a label i think) : Like calculate resolution, reduce it....
I try a lot of code fought in internet but no one works on me.
I try this alone but it display nothing :
voidmain() { QImage myImage; myImage.load("Revolt.jpg"); }
[edit koahnig: introduction of code wrappers]
-
Hi and welcome to devnet
Did you see this image viewer example?
Probably the best is to study that example and see what you have to change for purposes.
-
Thanks for your time.
I read the tutorial, i stopped at this code :
The function loadFile() is used to load the image.
bool ImageViewer::loadFile(const QString &fileName) { QImageReader reader(fileName); reader.setAutoTransform(true); const QImage newImage = reader.read(); if (newImage.isNull()) { QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1: %2") .arg(QDir::toNativeSeparators(fileName), reader.errorString())); return false; }
I tried this an i got a lot of error...
Can i just have a little code for just display a picture with a label if you can please ?
-
@Payx Hi! Showing an image with a
QLabel
is super easy:In your mainwindow.cpp just add the following:
// ... #include <QPixmap> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QPixmap pm("C:/Users/Patrick Wieland/Documents/Kochen/bento.jpg"); // <- path to image file ui->label_2->setPixmap(pm); ui->label_2->setScaledContents(true); } // ...
-
You need to add
#include <QPixmap>
to the other includes and add these three lines...QPixmap pm("C:/Users/Patrick Wieland/Documents/Kochen/bento.jpg"); // <- path to image file ui->label_2->setPixmap(pm); ui->label_2->setScaledContents(true);
to your existing MainWindow constructor.
-
Did you put in the correct path to your image file? The path in the code above is only valid on my harddrive ;-) Also, did you add a QLabel to your MainWindow? I added a QLabel with the designer in Qt Creator and the name of that label is label_2.
-
Only type "C:/Users/David/Documents/addddd/r.jpg", without the "file://". The pixmap constructor takes a file path and not an URL.
-
@Payx Add another QLabel (here its name is label_3) to the MainWindow. We can use that label to display the size of the Pixmap. When adding the label in Qt Creator's Designer, make the label wide enough so that all the text fits in.
In the MainWindow constructor in mainwindow.cpp:
const QSize s = pm.size(); // <- get size of the Pixmap ui->label_3->setText( QString("Size: %1, %2").arg(s.width()).arg(s.height()) );
Edit: Pixmap has a lot of built-in stuff, see QPixmap Class for more. QPixmap is there to display images, to actually put them on the screen. If you want to manipulate an image, you don't use a pixmap for that. Use a QImage then, see: QImage Class.
So, this is the workflow:
- Create a QImage from a file:
QImage img("C:/path/filename.jpg")
; - Do something with that image, like change the color of some pixels.
- Create a QPixmap from that QImage:
QPixmap pm = QPixmap::fromImage(img);
- Show the Pixmap in a QLabel:
ui->label->setPixmap(pm);
- Create a QImage from a file:
-
@Payx said in How to display a picture:
QImage::size()
It returns a
http://doc.qt.io/qt-5/qsize.html
and you can ask it height / width etc
http://doc.qt.io/qt-5/qsize.html#heightconst QSize MySize = theImage->size();
int h=MySize.height();
etc.
Update:
@wieland already showed ;) -
@Payx
Well You need to convert to text anyway so it will becomeui->label_3->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.width()) );
Not sure its better.
- Thanks but how to return it ?
What we do there is to convert to text and set that text in some label.
That is not really returning it.
returning it would be something like
QSize SomeFunc() {
return pm.size();
} - Thanks but how to return it ?
-
Thanks it works. I prefer : ui->label_3->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.height()) );
because i understand all.Then if i want to use height and width for other calcul i can use :
s.width() * 5 ?If i want to cut my picture in many square or rectangle i have to do :
int h = s.width() and work with h thats right ?