[Development] Why does QLabel::pixmap() return `const QPixmap*`?

Yves Bailly yves.bailly at laposte.net
Sun Nov 30 19:21:35 CET 2014


On 30/11/2014 09:35, Olivier Goffart wrote:
> On Sunday 30 November 2014 06:37:11 Kevin Kofler wrote:
>> Sze Howe Koh wrote:
>>> I'm curious about the rationale behind this API design. I can't think
>>> of any other Qt function that returns an implicitly-shared object by
>>> pointer, so this seems inconsistent. e.g. QWidget::font() returns a
>>> QFont by const-ref.
>>
>> Probably because the pointer can be null. C++ does not allow null
>> references. (The compiler might let you get away with it, but in principle,
>> it is not allowed.)
>
> That's not the reason because you can return a null pixmap (QPixmap::isNull).

However a null pixmap and no pixmap at all are two different things :-)

QPixmap const* pix = label->pixmap();
if ( pix == nullptr )
{
   // this label has no pixmap
}
else if ( pix->isNull() )
{
   // this label does have a pixmap, which is null/empty
}
else
{
   // etc.
}

Because of the different meanings, the API seems sensible to me.

Regards,

-- 
(o< | Yves Bailly                          | -o)
//\ | Linux Dijon  : http://www.coagul.org | //\
\_/ |                                      | \_/`



More information about the Development mailing list