2017-05-07 305 views
0

我已经使用我们怎样才能使一个QRubberBand半透明

setOpacity(); 

setAttribute(Qt:WA_TranseculentBackground:) 

连我都绑了所有可用的解决方案什么事都没有效果。

这是我的代码

void Physician::mouseMoveEvent(QMouseEvent *e) 
{ 
    rubberBand->hide(); 
    bottomRight = e->pos(); 
    QRect rect = QRect(topLeft, bottomRight); 
    rubberBand->setGeometry(rect);//Area Bounding 
    QToolTip::showText(e->globalPos(), QString("%1,%2") 
     .arg(rubberBand->size().width()) 
     .arg(rubberBand->size().height()), this); 
} 
void Physician::mousePressEvent(QMouseEvent *e) 
{ 
    rubberBand->hide(); 
    if(e->x()<ui->videoShowLabel->x()||e->y()<ui->videoShowLabel->y()) 
    { 
     selectWithInLabel.critical(0,"Error", "Select within the LABEL !"); 
     selectWithInLabel.setFixedSize(500, 200); 
    } 
    else{ 
     topLeft = e->pos(); 
     myPoint = ui->videoShowLabel->mapFromGlobal(this->mapToGlobal(e->pos())); 
    } 

} 
void Physician::mouseReleaseEvent(QMouseEvent *e){ 
    rubberBand->setWindowOpacity(0.5); 
    rubberBand->show(); 
} 

void Physician::on_manualROIRadioButton_clicked() 
{ 
    rubberBand = new RubberBand(RubberBand::Rectangle, this); 
} 

我应该做些什么,使橡皮半透明

+0

难道这一切还是相关[这个问题](http://stackoverflow.com/questions/43559562/qrubberband-and-mplayer-在-qlabel)? –

+0

是的,这是所有仍然有关这个问题 –

回答

0

使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); 
+0

是否有任何解决方案,使半透明,如果我不做任何外部类 –

+0

创建一个'QRubberBand'作为顶级窗口(即没有父),并调用'setWindowOpacity '。但是,它将由您来管理其几何。 –