Chapter 11. Some fixes here and there

Fixing the color button.

As you may have noticed, when the user changes the color in the color button, the sliders change its values, but not the other way round. So we're going to fix it.

We add m_colorButton->setColor(m_color); to CentralView::updatePixmap and that makes m_colorButton update its value at the same time than we update the pixmap.

You may want to update m_colorButton each time one slider changes, but if you do that, you may find some problems and you may have to use QObject::blockSignals to block either the sliders signals or m_colorButton's, so we just update it in updatePixmap and keep it safe.

Fixing the initial pixmap used

First, we're going to remove that Qt logo that m_label uses by default. To do so, open centralviewbase.ui in the designer, select the label and remove the contents of the pixmap property by clicking on the small red arrow (it appears when selecting the property).

pixmap property

Anyway, we want to have something painted in the label since the beginning. So we add a call to updatePixmap(); in CentralView's constructor.

Still, m_pixmap is empty when TheTinter is started, and we want to do something nevertheless. We're going to modify updatePixmap to:

void CentralView::updatePixmap()
{
  m_colorButton->setColor(m_color);
  QPixmap pixmap;
  if (!m_pixmap.isNull())
  {
    QImage image= m_pixmap.convertToImage();
    KImageEffect::blend(m_color, image, m_alpha->value()/100.0 );
    pixmap.convertFromImage(image);
  }
  else
  {
    pixmap = QPixmap(128,128);
    pixmap.fill(m_color);
  }
  m_label->setPixmap(pixmap);
}

That way, we first check if m_pixmap is valid (is not null) and in that case, we do everything as before. In case m_pixmap hasn't been initialized, then we do what we did in the first steps of the tutorial and just paint a flat color.