How to update a QLabel to display a QImage - c++

I may be misunderstanding how to do this, hopefully somebody can clarify.
I have placed a QLabel on my main window from inside Qt Designer. Currently, it just displays the letters "TextLabel" when I run the program.
I have a line edit where I type in the path to an image, which then loads the image as a QImage into memory (I have this working).
Finally, I want to display this QImage. I was told this can be done by QLabels. I have done the following, but nothing is happening:
ui.input_label->setPixmap(QPixmap::fromImage(my_qimage));
Why is nothing happening? How can I display this image?

If you are sure the image file is ok but then the problem is probably with loading the file into the QImage. If you are calling QImage::load(), you are probably getting a return value of false.

Related

Qgraphicview resizing when windows resizing

I would like to ask you about an issue I do not succeed to fix.
I have a QApplication which load an image in the first mainwindow.
The code is the following:
QGraphicsScene *scene = new QGraphicsScene;
QPixmap pixmap(QString::fromStdString("image.png");
scene->addPixmap(pixmap);
ui->graphview->setScene(scene);
ui->graphview->show();
I am trying to fit the image to scale in the QGraphivsView, even when I resized my windows.
However, the image is displayed with its own size at running time and this size is not changing when windows is resized. For example, increasing my windows does not increase the image size and similarly for decreasing.
I tried even by addind the following code:
ui->graphview->fitInView(pixmap, Qt::KeepAspectRatioByExpanding);
But nothing is working.
I provide you an example on the following image of what is happening.
I have found a way however, it is not 100% satisfying.
I have replaced the resizeEvent function as on the example below:
void QMainWindows::resizeEvent(QResizeEvent *){
QRectF bounds = ui->graphQSYS->scene()->sceneRect();
ui->graphQSYS->fitInView(bounds, Qt::KeepAspectRatioByExpanding);
ui->graphQSYS->centerOn(bounds.center());
}
However, when resizing, the image quality is becoming really bad. Text written on it cannot be read anymore. Do you know another way to keep picture quality ?
Thank you very much.

How do I set a background image to mainwindow?

I am trying to set the background image of the mainwindow through setStyleSheet() but this also applies the image to its children including textbrowsers and TextEdit. How can I accomplish this without doing so?
Try to add What you have tried so far,with out that we cant help.
Try the below one
setStyleSheet("MainWindow {background-image:url(:/files/MainPic/Technology-Banner1.jpg)}");

Qt - QPixmap converts png to gray scale?

In Qt I used the following method to load images and set them as the background of QLabels:
QPixmap *pixmap= new QPixmap("home\\Images\\circle.png");
ui->cell11_Image->setPixmap(*pixmap);
ui->cell11_Image->setMask(pixmap->mask());
ui->cell11_Image->show();
delete(pixmap);
With the following result
The thing is: the images are in grayscale. However, here is one of the images
Does anyone know what is going on?
I think your label or one of parent widget is disable. Try to enable and check.

Does imshow() have something to do with label->setPixmap()?

Here is a piece of code I written.
image = Mat2QImage(frame2Copy);
ui->label->setPixmap(QPixmap::fromImage(image).scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio));
imshow("frame2Copy", frame2Copy);
You can see that I want to translate a frame(cv::Mat) into an QImage, and then display it in a QLabel. The problem is that I can not comment the last line code:
imshow("frame2Copy", frame2Copy);
If this line of code is commented, the program will crashed. I do not need the frame to show in another new opened window since I have displayed it in a QLabel. However, I cannot remove this code. I can't figure out why. Can someone help me? Thanks very much.

Newbie: Minimal program to display a PNG in a window

All,
I must have a fundamental neuron missing, but I cannot get a simple program to load a PNG file and display it in a window. I'm not sure if it is a QPixmap, a QPicture, or what. All of the samples in the QTCreator are a bit more than I need right now. Baby steps...
I can get the window to display, and the program doesn't barf when I try to load the PNG, but it never gets displayed.
If someone would post a simple program to load a PNG from a file and display it, it would greatly appreciated. (I know, asking a lot, but...).
Thanks!
:bp:
this example is minimal: http://doc.trolltech.com/4.6/widgets-imageviewer.html
You will want to have a look at the function ImageViewer::open():
Build a QImage object from a filename;
Convert your QImage to a QPixmap with QPixmap::fromImage();
Put your QPixmap in a QLabel with QLabel::setPixmap().
The QImage object will automatically chose an appropriate reader according to the format of the image it detects in step 1.