How to display an image at full width in Qt QGraphicsView? - c++

I'm using this code to load image and show in screen.
void MainWindow::on_actionOpen_triggered(void){
QString fileName;
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "C:\\Users", tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
qim.load( fileName );
pixmap = QPixmap::fromImage( qim );
scene.clear();
scene.addPixmap( pixmap );
ui->graphicsView->setScene(&scene);
}
But when I upload a small picture, it doesn't fill the screen and it appears in its original size. As you can see:
The desired screen should look like this:
(I am trying to make an application similar to this site. Grayscale doesn't matter.)
image used:

Try fitInView with Qt::AspectRatioMode on your ui->graphicsView object.

This solved my problem:
qim.load( fileName );
pixmap = QPixmap::fromImage( qim );
QPixmap scaled_img = pixmap.scaled(this->width(), this->height(), Qt::KeepAspectRatio);
scene.clear();
scene.addPixmap( scaled_img );
ui->graphicsView->setScene(&scene);

Related

How to save view ( in .jpeg) with white background in Qt?

I am having a QGraphicsView, which contains many QGraphicsItem. I have a one feature, which saves, current view in .jpeg format.
But the problem is picture's background is completely black irrespective of view's background color.
How to make picture's background color to white ?
Here is my code:
void myClass::SavePic()
{
QString fName = QFileDialog::getSaveFileName(this, tr("Save File"),
"/myFiles/Qt",
tr("Images (*.png *.xpm *.jpg)"));
if(fName.isNull())
return;
QRect rect = _scene->sceneRect().toAlignedRect();
QImage image(rect.size(),QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
scene->render(&painter);
image.save(fileName);
}

QTextDocument::DrawContents skips resources?

I have this setup:
// ...
// variable document is a QTextDocument* which has some 'RichText' + 'Images'
QTextEdit textEdit;
textEdit.setDocument(document);
textEdit.setLineWrapMode(QTextEdit::LineWrapMode::NoWrap);
auto image = QImage(document->size().width(), document->size().height(),
QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter painter(&image);
document->drawContents(&painter);
// ...
I'm doing this to have my text rendered in a long horizontal QImage (hence the "NoWrap" LineWrapMode), so I can select a small part of it at a time with QImage::copy(QRect) and create a scrolling text effect.
The reason I'm doing it this way is that I need to have a QImage at the end which then I would feed its buffer (QImage::bits()) to the hardware that I'm using as my final output.
So it works great, it displays formatted text with fonts and colors and everything except for the images, it seems to skip them, notice the file icon in "result of text with image" picture.
This is text only in editor
This is result of text only
This is text with image in editor
This is result of text with image
This is how I'm inserting images to my QTextDocument:
QImage image(url.toLocalFile());
if (image.isNull())
return;
image = image.scaledToHeight(getDocumentHeight(), Qt::SmoothTransformation);
auto filename = QUrl(url.fileName());
textEdit->document()->addResource(QTextDocument::ImageResource, filename, image);
textEdit->textCursor().insertImage(filename);
So I don't think it's because "DrawContents" fails to find the image resource file or something like this.
What should I do? Is there something that I'm missing? Any kind of help in the matter is highly appreciated! ;)
In the following code I show how an image should be loaded, then save it to a file, probably the error is that you have not finished painting, for this you must call painter.end() or delete painter from memory.
main.cpp
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
QVBoxLayout vlayout(&widget);
QTextEdit textEdit;
QPushButton button("save image");
QPushButton loadButton("Load and Insert");
vlayout.addWidget(&loadButton);
vlayout.addWidget(&textEdit);
vlayout.addWidget(&button);
widget.show();
textEdit.append("some text");
QObject::connect(&loadButton, &QPushButton::clicked,[&textEdit](){
QString filename = QFileDialog::getOpenFileName(&textEdit, "Select", "", "*.png");
if(!filename.isEmpty()){
QImage image(filename);
QUrl url = QUrl::fromLocalFile(filename);
image = image.scaledToHeight(100, Qt::SmoothTransformation);
textEdit.document()->addResource(QTextDocument::ImageResource, url, image);
textEdit.textCursor().insertImage(image);
}
});
QObject::connect(&button, &QPushButton::clicked, [&textEdit](){
QImage image(textEdit.document()->size().toSize() , QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter painter(&image);
textEdit.document()->drawContents(&painter);
painter.end();
image.save("image.png");
});
return a.exec();
}

Loading sf::Image by QDialog

Is there a easy way to load sf::Image using for example QDialog or other library? I mean I want to load image for using it in sfml by "Choose image..." window.
You can use QFileDialog to get the path of the given image:
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
QDir::currentDirPath() ,
tr("Images (*.png *.xpm *.jpg)"));
if (!fileName.isEmpty()) {
// Now you can init your sf::image
}
According to the official documentation of SFML, you can use the loadFromFile function
sf::Image image;
if (image.loadFromFile(filename.toStdString())) {
std::cout << "image loaded with success";
}

When i open a image with imread function of OpenCV, why does it has a border?

source image:
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),".",
tr("Image Files(*.png *.jpg *.jpeg *.bmp)"));
image = cv::imread(fileName.toUtf8().data());
if(!image.data){
qDebug()<< "Could not open or find the image";
return ;
}
QString status = QString::number(image.rows) + "x" +
QString::number(image.cols);
ui->label_2->setText(status);
//show
cv::namedWindow("Original Image");
cv::imshow("Original Image",image);
image result:
I can't understand this situration,Where is the problem?

QImage doesn't seem to load any images at all (always returning null)?

I'm on Windows7 and using Qt SDK 4.8.
Trying to read a file in with QImage but it just doesn't seem to load. That is, QImage(filename) or QImage(filename, "PNG") or QImage.load(filename) always return NULL.
Here's my code:
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),
QDir::homePath(),
tr("Image Files (*.png *.tga *.bmp)"));
if (!fileName.isEmpty())
{
targetImage = new QImage(fileName, "PNG");
if(targetImage->isNull())
{
QMessageBox::information(this,
tr("PhotoChop"),
tr("Cannot load %1.").arg(fileName));
return;
}
onScreenImage.setBackgroundRole(QPalette::Base);
onScreenImage.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
onScreenImage.setScaledContents(true);
onScreenImage.setPixmap(QPixmap::fromImage(*targetImage));
}
}
What am I doing wrong?
it has to do with how QImage loads "plugins" for reading files in. One option is to enable these plugins (for different file formats) and then linking against them.
Though instead I just used Gdiplus::Bitmap (from windows) to load in the file and then, from that, created an HBITMAP which QPixmap can use.