C++ Qt橡胶带选择矩形透明

C++ Qt橡胶带选择矩形透明,c++,qt,qt5,C++,Qt,Qt5,如何使橡皮筋选择透明?我尝试了此代码,但不起作用: void RubberBand::mousePressEvent(QMouseEvent* event) { if (event->buttons() != Qt::LeftButton) return; if (rubberBand.isVisible()) { rubberBand.hide(); return; } auto posItem = Relat

如何使橡皮筋选择透明?我尝试了此代码,但不起作用:

void RubberBand::mousePressEvent(QMouseEvent* event) { 
    if (event->buttons() != Qt::LeftButton) return; 
    if (rubberBand.isVisible()) {
        rubberBand.hide(); 
        return; 
     } 
     auto posItem = RelativeClippedCoordinates(event->globalPos()); 
     origin = CoordinatesItemToGlobal(pixmapItem, posItem); 
     selectionRect.setTopLeft(posItem); 
     rubberBand.setGeometry(QRect(origin, QSize())); 
     rubberBand.setStyleSheet("background-color:trasparent;"); 
     rubberBand.show(); 
}

为了执行此任务,我们必须覆盖
paintEvent
方法并激活
Qt::WA_TranslucentBackground
属性

customrubberband.h

#ifndef CUSTOMRUBBERBAND_H
#define CUSTOMRUBBERBAND_H

#include <QRubberBand>

class CustomRubberBand : public QRubberBand
{
public:
    CustomRubberBand(Shape s, QWidget * p = 0);

protected:
    void paintEvent(QPaintEvent *);
};

#endif // CUSTOMRUBBERBAND_H
#include <QRubberBand>
[...]
QRubberBand rubberBand;


完整代码:

void RubberBand::mousePresseEvent(QMouseEvent*event){if(event->buttons()!=Qt::LeftButton)return;if(RubberBand.isVisible()){RubberBand.hide();return;}auto posItem=相对立轴坐标(event->globalPos());origin=坐标项目全局(pixmapItem,posItem);selectionRect.setTopLeft(posItem);rubberBand.setGeometry(QRect(origin,QSize());rubberBand.setStyleSheet(“背景色:trasparent;”);rubberBand.show();}上面是我的code@mahesht请先读一篇文章,然后再学习你写的文章。你写的是
color:trasparent
,而不是
color:transparent
。当问题陈述仅仅是“它不起作用”时,很难提供解决方案。请你的问题更完整地描述一下你预期会发生什么,以及这与实际结果有什么不同。请参阅,以获取关于什么是好的解释的提示。我尝试了,但没有成功。背景颜色仍然是蓝色。我添加了图像。单击“橡胶带透明”检查此github链接。您将获得所有代码。我想让选择部分从橡胶带透明。如果你有任何想法,请帮助我。我正在使用5.7.1版本
#include <QRubberBand>
[...]
QRubberBand rubberBand;
#include "customrubberband.h"
[...]
CustomRubberBand rubberBand;