QPixmap Problem
-
I have used QPixmap before successfully, I am stuck on this one. I am trying to create a PNG file that I can use in another application, but all it seems to produce is a black image. The code is:
if (mapCreated) return; parcelMap = new QPixmap(20000, 10000); parcelMap->fill(Qt::white); QPainter pm(parcelMap); static const QPointF points1[5] = { QPointF(200, 200), QPointF(20000-200, 200), QPointF(20000-200, 5000-100), QPointF(200, 5000-100), QPointF(200, 200)}; pm.drawPolyline(points1, 5); static const QPointF points2[5] = { QPointF(200, 5000+100), QPointF(10000-100, 5000+100), QPointF(10000-100, 5000-100), QPointF(200, 5000-100), QPointF(200, 200)}; pm.drawPolyline(points2, 5); static const QPointF points3[5] = { QPointF(10000+100, 5000+100), QPointF(20000+200, 5000+100), QPointF(20000+200, 5000-100), QPointF(10000+100, 5000-100), QPointF(10000+100, 200)}; pm.drawPolyline(points3, 5); QFile file("//home//XXX/Pictures//parcels.png"); file.open(QIODevice::WriteOnly); parcelMap->save(&file, "PNG"); mapCreated = true;
The file is created, but the Linus Image Viewer shows it black. Any thoughts on what I am doing incorrectly?
Second question: If I have a pixmap that is larger in extent than my widget, will the pixmap image be shrunk to fit the widget if I use
drawPixmap(widget->rect, pixmap, pixmap->rect)
(I believe that I will need to keep the aspect ratio)? -
- Are you using the paintEvent() function?
- Depends, check enum Qt::AspectRatioMode in Documentation.
-
@ofmrewThanks, but let me explain: In the snippet above I only created the file, which I did because I got confusing results when I used paintEvent to the display the pixmap on the widget.
I do not require a paintEvent when drawing on a pixmap. The statement about the aspect ration was an aside, I knew that I would need to use something like what you pointed out or do it myself as I have done in the past.
My real two qestions are: 1. Does the drawPixmap scale the image. 2. And most important, why do I get just a black image.
-
Ok, I have a program that create a .png correctly... For reference while I got the answer:
QApplication app( argc, argv ); QPixmap pixmap( 200, 200 ); pixmap.fill( Qt::white ); QPainterPath path; path.addEllipse( 80, 80, 80, 80 ); path.moveTo( 120, 120 ); path.lineTo( 120, 40 ); path.arcTo( 40, 40, 160, 160, 90, 90 ); path.lineTo( 120, 120 ); QFont font = QApplication::font(); font.setPixelSize( 40 ); path.addText( 20, 180, font, "Path" ); QPainter painter( &pixmap ); painter.setRenderHint( QPainter::Antialiasing ); painter.setPen( Qt::black ); painter.setBrush( Qt::gray ); painter.drawPath( path ); pixmap.save( "path.png" ); return 0;
-
Hi,
You forgot to call
pm.end()
.By the way since you don’t show that image you should rather use QImage as it doesn’t require a window server running.
-
Let me pose my question another way. What I want to do is to draw three rectangles on to something that I can save to a file in read into another application as a background, much like you see an aerial photograph overlaid with a road network. I my case, I will be digitizing the road network using the aerial photograph as a reference. I am using QPixmap, but should I be using QImage?
-
QPixmap is optimised for showing on screen. If you don't plan to do that then go on with QImage.
-
@Charlie_Hdz I had to convert your program into a console application, but I got nothing. How did you creat yours, I did mine with QtCreator.
Here is mine:
#include <QCoreApplication>
#include <QPainter>
#include <QPainterPath>
#include <QFont>
#include <QPixmap>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);;QPixmap pixmap( 200, 200 ); pixmap.fill( Qt::white ); QPainterPath path; path.addEllipse( 80, 80, 80, 80 ); path.moveTo( 120, 120 ); path.lineTo( 120, 40 ); path.arcTo( 40, 40, 160, 160, 90, 90 ); path.lineTo( 120, 120 );
// QFont font = QCoreApplication::font();
// font.setPixelSize( 40 );// path.addText( 20, 180, font, "Path" );
QPainter painter( &pixmap ); painter.setRenderHint( QPainter::Antialiasing ); painter.setPen( Qt::black ); painter.setBrush( Qt::gray ); painter.drawPath( path ); pixmap.save( "//home//bobwilliams//Pictures//path.png" ); return a.exec();
}
-
I did a Qt Widget Application. I commented as you did and it worked. Check whether your path is correct.
-
@Charlie_Hdz I change to what created the parcels.png file and got nothing. I do not see what is wrong it is very simple. I will try reducing the size and try with both QPixmap and QImage, then I will post the results.
In what folder did the file from pixmap.save( "path.png" ); end up.
-
@ofmrew said in QPixmap Problem:
In what folder did the file from pixmap.save( "path.png" ); end up.
Depends, it can be saved at Home(root directory) or in the project output folder. I tried in Ubuntu.
-
@ofmrew said in QPixmap Problem:
I had to convert your program into a console application
So, hadn't have an error when QApplication header was not detected? In the console application, GUI is removed... So it causes errors.
Create a Qt Widget Application, then insert the code and compile. It should work in that way!
-
What I meant was when you executed the code you posted, in what folder did you find the path.png file?
-
This one is working fine. I get a white image with several rectangles on it
#include <QCoreApplication> #include <QFile> #include <QImage> #include <QPainter> #include <QPointF> #include <QtDebug> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QImage parcelMap(20000, 10000, QImage::Format_ARGB32); parcelMap.fill(Qt::white); QPainter pm(&parcelMap); static const QPointF points1[5] = { QPointF(200, 200), QPointF(20000-200, 200), QPointF(20000-200, 5000-100), QPointF(200, 5000-100), QPointF(200, 200)}; pm.drawPolyline(points1, 5); static const QPointF points2[5] = { QPointF(200, 5000+100), QPointF(10000-100, 5000+100), QPointF(10000-100, 5000-100), QPointF(200, 5000-100), QPointF(200, 200)}; pm.drawPolyline(points2, 5); static const QPointF points3[5] = { QPointF(10000+100, 5000+100), QPointF(20000+200, 5000+100), QPointF(20000+200, 5000-100), QPointF(10000+100, 5000-100), QPointF(10000+100, 200)}; pm.drawPolyline(points3, 5); pm.end(); QFile file("parcels.png"); if (!file.open(QIODevice::WriteOnly)) { qDebug() << "Failed to open file" << file.errorString(); } parcelMap.save(&file, "PNG"); return 0;
-
There's an upload image button just above the editor, third from the right.
-
@SGaist I have narrowed the problem to my Mint Linux computer because I moved the one above to a Win 10 computer and I got a white rectangle with just visible rectangles. I moved the .png file to the Win 10 machine and, guess what, it showed a white rectangle with just visible rectangles. So the problem appears to be Image Viewer. Which viewer do you use on Linux computers.
As for the icons on the editor, to tell the truth, I never really noticed them. Is the some documentation on their meaning and how to use them. Most are obvious, but link, picture, zen mode and the one on the end, require more explanation.
Seems we spent all this time running to ground a non-problem with Qt, while the real culprit was software provided by the operating system; however, just for grins I tried GIMP Image Viewer and of course it showed the image correctly. Warning to Mint Linux users, Image Viewer is not to be trusted.
Thanks to all for the help.
-
It depends on the desktop environment so there are several possible like Gwenview, Okular, etc.
Zen mode simply puts the editor in full screen so you don't have any distraction and the last one on the right allows to easily insert code sample.