使QRubberband
半透明的真正问题是mplayer
在没有任何Qt知识的窗口上绘制。因此Qt本身不能作为合成器来产生所需的效果。
一种可能性是使QRubberBand
成为顶级窗口。这样合成是底层图形系统的责任,而不是Qt。
考虑到这一点,请尝试以下操作。首先一个实用程序基类来管理几何...
class abstract_rubber_band {
public:
virtual QRect get_geometry() const
{
return(QRect(m_parent->mapFromGlobal(widget().geometry().topLeft()), widget().size()));
}
virtual void set_geometry (const QRect &rect)
{
widget().setGeometry(map_rect(rect));
}
protected:
explicit abstract_rubber_band (QWidget *parent)
: m_parent(parent)
{}
/*
* @param point Coords relative to effective parent.
*/
QPoint map_point (QPoint point) const
{
if (point.x() < 0)
point.rx() = 0;
else if (point.x() >= m_parent->width())
point.rx() = m_parent->width() - 1;
if (point.y() < 0)
point.ry() = 0;
else if (point.y() >= m_parent->height())
point.ry() = m_parent->height() - 1;
point = m_parent->mapToGlobal(point);
return(point);
}
QRect map_rect (QRect rect) const
{
return(QRect(map_point(rect.topLeft()), map_point(rect.bottomRight())));
}
private:
QWidget &widget()
{
return(dynamic_cast<QWidget &>(*this));
}
const QWidget &widget() const
{
return(dynamic_cast<const QWidget &>(*this));
}
QWidget *m_parent;
};
现在使用上述作为所需橡胶带类的碱...
class rubber_band: public abstract_rubber_band,
public QRubberBand {
using super = QRubberBand;
public:
/*
* @param parent Note that this isn't actually used as the
* parent widget but rather the widget to which
* this rubber_band should be confined.
*/
explicit rubber_band (QWidget *parent)
: abstract_rubber_band(parent)
, super(QRubberBand::Rectangle)
{
setAttribute(Qt::WA_TranslucentBackground, true);
}
protected:
virtual void paintEvent (QPaintEvent *event) override
{
QPainter painter(this);
painter.fillRect(rect(), QColor::fromRgbF(0.5, 0.5, 1.0, 0.25));
QPen pen(Qt::green);
pen.setWidth(5);
painter.setPen(pen);
painter.setBrush(Qt::NoBrush);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
/*
* Display the current geometry in the top left corner.
*/
QRect geom(get_geometry());
painter.drawText(rect().adjusted(5, 5, 0, 0),
Qt::AlignLeft | Qt::AlignTop,
QString("%1x%2+%3+%4").arg(geom.width()).arg(geom.height()).arg(geom.left()).arg(geom.top()));
}
};
上面rubber_band
类应该几乎可以代替QRubberBand
。主要区别在于,您不能使用geometry
/setGeometry
读取/写入其几何图形,而必须使用get_geometry
/set_geometry
- 这些将执行到全局坐标的映射。
你的具体情况创建与rubber_band ...
rubberBand = new rubber_band(ui->videoShowLabel);
难道这一切还是相关[这个问题](http://stackoverflow.com/questions/43559562/qrubberband-and-mplayer-在-qlabel)? –
是的,这是所有仍然有关这个问题 –