ピックスマップをペイントするQLabelのサブクラスであるウィジェットからマウスオーバーイベントをキャプチャしようとしています。たとえば、不透明度を 50% に設定して透明効果を作成するには、このイベントをキャプチャする必要があります。私はsetWindowOpacity(0.5)
成功せずに試しました。
問題は、QLabel のサブクラスであるウィジェットに描画された画像の不透明度を変更するにはどうすればよいかということです。
PaintWidget.cpp
void PaintWidget::paintEvent(QPaintEvent *aEvent)
{
QLabel::paintEvent(aEvent);
if (_qpSource.isNull()) //no image was set, don't draw anything
return;
float cw = width(), ch = height();
float pw = _qpCurrent.width(), ph = _qpCurrent.height();
if (pw > cw && ph > ch && pw/cw > ph/ch || //both width and high are bigger, ratio at high is bigger or
pw > cw && ph <= ch || //only the width is bigger or
pw < cw && ph < ch && cw/pw < ch/ph //both width and height is smaller, ratio at width is smaller
)
_qpCurrent = _qpSource.scaledToWidth(cw, Qt::FastTransformation);
else if (pw > cw && ph > ch && pw/cw <= ph/ch || //both width and high are bigger, ratio at width is bigger or
ph > ch && pw <= cw || //only the height is bigger or
pw < cw && ph < ch && cw/pw > ch/ph //both width and height is smaller, ratio at height is smaller
)
_qpCurrent = _qpSource.scaledToHeight(ch, Qt::FastTransformation);
int x = (cw - _qpCurrent.width())/2, y = (ch - _qpCurrent.height())/2;
QPainter paint(this);
paint.drawPixmap(x, y, _qpCurrent);
}
void PaintWidget::setPixmap(const QPixmap& pixmap)
{
_qpSource = _qpCurrent = pixmap;
repaint();
}