user3167959
user3167959

Reputation: 31

how to load image to painter.drawimage function

I have stored the images in board local directory(/home/images/system/image.png)

for painter.drawimage function pass the Qpoint and Qimage as parameter.

painter.drawImage(QRect(100, 50, 100, 100), QImage("home/images/system/image.png"));

Can I directly use path for image here?? If I load like this it not working Is this correct way to pass the path of the image which is stored in board directory?

Upvotes: 1

Views: 11147

Answers (3)

Wagmare
Wagmare

Reputation: 1406

Instead of

painter.drawImage(QRect(100, 50, 100, 100), QImage("/home/images/system/charge1.png"));

you can use

QImage image("home/images/system/charge1.png");
painter.drawImage(rect, image);

In the documentation you can see

QRectF target(10.0, 20.0, 80.0, 60.0);
QRectF source(0.0, 0.0, 70.0, 40.0);
QImage image(":/images/myImage.png");

QPainter painter(this);
painter.drawImage(target, image, source);

at the link: http://qt-project.org/doc/qt-4.8/qpainter.html#drawImage-5

Also, I recommend you use resource files in Qt:

Upvotes: 0

László Papp
László Papp

Reputation: 53173

You can see yourself the inconsistency between the location and the path you are trying to load:

I have stored the images in board local directory(/home/images/system/image.png)

and then:

painter.drawImage(QRect(100, 50, 100, 100), QImage("home/images/system/image.png"))

You are missing the leading slash for the absolute path. Why your program currently does not work it is because the program is looking for the image relative to the application as opposed to the absolute path.

The solution would be to make a minor fix to the aforementioned line as follows:

painter.drawImage(QRect(100, 50, 100, 100), QImage("/home/images/system/image.png"))
                                                    ^

However, this is just a quick workaround. In an ideal world, you would not really load images like that, and especially not from different home folders than the user running the application.

If you used the same user's home directory, you would not use a hard coded path, but the following option from QStandardPaths:

QStandardPaths::HomeLocation    8   Returns the user's home directory.

It is a very bad practice to hard code a long absolute path like that unless really necessary, but that is a rarity.

Alternatively, you could also put the image next to the application, and load it the following way:

painter.drawImage(QRect(100, 50, 100, 100),
                  QImage(QString("%1/image.png")
                  .arg(QCoreApplication::applicationDirPath()));

As per the QCoreApplication documentation for this method.

I would probably stick to the latest option, or at least the aforementioned home path without hard coding.

Of course, you could always use the Qt resource system as well, but I think that would be a bit excessive in this simple case unless you would like to make the image is bundled up so that you do not need to deploy it separately. In this case, you would be writing something like this:

painter.drawImage(QRect(100, 50, 100, 100), QImage(":/image.png"));

and you would have your resource file in your .pro qmake file as follows:

RESOURCES += myresourcefile.qrc

and the resource file would contain something like this:

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
    <file>image.png</file>
</qresource>
</RCC>

Upvotes: 1

Marek R
Marek R

Reputation: 37607

  1. you are missing front slash
  2. loading image in paint event is bad approach
  3. try this:

.

 QImage img("/home/images/system/image.png");
 Q_ASSERT(!img.isNull());
 painter.drawImage(QRect(100, 50, 100, 100), img);

Upvotes: 0

Related Questions