我正在尝试从一个小部件捕获鼠标悬停事件,该小部件是 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();
}