0
votes

Good morning all,

I am having trouble adding a .PNG file to my Qt Creator project using the QPixmap and QLabel method. The different ways I've tried to do this (in code snippet format) appear below.

System: Raspberry Pi 2 running Debian Wheezy
Builder: Qt Version 4.8.2 For Embedded Linux
Compiler/Tool Chain: GCC/G++ 4.9.2

code snippet 1
1 #include <QtGui/QApplication>
2 #include <QPixmap>
3 #include <QLabel>
4 #include <wiringPi.h>
5 #include "mainwindow.h"
...
187 QLabel myLabelPi;
188 QPixmap myPixmapPi = new QPixmap(":/GPIOGUI/rpi2.png");
189 myLabelPi.setPixmap(myPixmapPi);
190 myLabelPi.setScaledContents(true);
191 myLabelPi.show();

code snippet 2
1 #include <QtGui/QApplication>
2 #include <QPixmap>
3 #include <QLabel>
4 #include <wiringPi.h>
5 #include "mainwindow.h"
...        
187 QLabel myLabelPi;
188 QPixmap *myPixmapPi = new QPixmap(":/GPIOGUI/rpi2.png");
189 myLabelPi.setPixmap(myPixmapPi);
190 myLabelPi.setScaledContents(true);
191 myLabelPi.show();

The errors I get with each of these codes when they compile (respectively) are:

code snippet 1:
/home/pi/gpioGUI/main.cpp:187: error: conversion from 'QPixmap*' to non-scalar type 'QPixmap' requested

code snippet 2:
/home/pi/gpioGUI/main.cpp:188: error: no matching function for call to 'QLabel::setPixmap(QPixmap*&)' candidate is: void QLabel::setPixmap(const QPixmap&) note: no known conversion for argument 1 from 'QPixmap*' to 'const QPixmap&'

Thank you very much for any help you can provide.

1
In your second example change myLabelPi.setPixmap(myPixmapPi); to myLabelPi.setPixmap(*myPixmapPi);drescherjm
Nobody seems to have pointed you to the better approach, which is not using a pointer for your QPixmap. You could initialize it with QPixmap myPixmap(":/GPIOGUI/rpi2.png");.thuga

1 Answers

1
votes

Read the errors carefully, most of the times it contains what is wrong.

Line 188 : new returns a pointer. Hence your variable should be a pointer (with a * )

QPixmap* myPixmapPi = new QPixmap(":/GPIOGUI/rpi2.png");

In line 189, it also says what's wrong. You're passing a pointer where a reference is expected. (I haven't tested the code though)

myLabelPi.setPixmap(*myPixmapPi);

Try this and see.