mardi 5 septembre 2017

How to transfer QPainter?

In a Qt-related code I tried to reduce some duplicated lines by the following method:

QPainter createPainter(const QColor& color)
{
  QPointer painter(&m_image);
  painter.setBrush(color);
  painter.setPen(color);
  return painter;
}

But QPainter's copy ctor is explicitly deleted. It should be not not problem in c++11, but sadly it is neither moveable. Is it any reason for this or just an API defect? Is it a good solution for createPainter?

  • It could accept a QPainter reference, but this would decrease the meaning of the method, and would add noise to the caller's code.
  • It could return a unique_ptr to the QPainter, but it would introduce overhead for heap-allocation.
  • ... Any other ideas?

Thanks for your responses!

Aucun commentaire:

Enregistrer un commentaire