简体   繁体   English

如何制作半透明颜色的 QGraphicsItem?

[英]How to make a QGraphicsItem of translucent color?

I am working on a QT GUI that annotates pictures from a graphics View via various shapes, currently developing the easiest one which is a rectangle.我正在开发一个 QT GUI,它通过各种形状从图形视图中注释图片,目前正在开发最简单的矩形。 I managed to add a rectangle to the image once a button is pressed and move it around with setFlag function.我设法在按下按钮后向图像添加一个矩形,并使用 setFlag 函数移动它。 What i need to do now is make sure the rectangle is translucent so that the user can see what is exactly annotated.我现在需要做的是确保矩形是半透明的,以便用户可以看到确切注释的内容。 My code for the rectangle button:我的矩形按钮代码:

void MainWindow::on_pushButton_11_clicked()  // rectangle shape creator
{
    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackpen(Qt::black);
    blackpen.setWidth(3);
    rectangle = scene->addRect(-100,-100,50,50,blackpen,blueBrush);
    rectangle->setFlag(QGraphicsItem::ItemIsMovable);
}

I have researched the Qt documentation and found that the opacity function from QGraphicsItem library would probably be the solution to this but I couldn't find a way to implement this.我研究了 Qt 文档,发现 QGraphicsItem 库中的 opacity 函数可能是解决方案,但我找不到实现这一点的方法。 Any help or suggestions are appriciated thanks.任何帮助或建议都非常感谢。

There are several solutions:有几种解决方案:

  • Use the setOpacity() method that will make the items transparent in the fill and the border color.使用setOpacity()方法使填充和边框颜色中的项目变得透明。

     rectangle->setOpacity(.2);
  • If you want to set the transparency in the fill color then you must set a transparent QColor to the QBrush.如果要在填充颜色中设置透明度,则必须为 QBrush 设置一个透明的 QColor。

     QColor brush_color(Qt::blue); brush_color.setAlpha(50); QPen blackpen(Qt::black); blackpen.setWidth(3); rectangle = scene->addRect(-100, -100, 50, 50, blackpen, brush_color); rectangle->setFlag(QGraphicsItem::ItemIsMovable);
  • If you want to set transparency at the border then you must set a transparent QColor to the QBrush.如果要在边框处设置透明度,则必须为 QBrush 设置一个透明的 QColor。

     QBrush blueBrush(Qt::blue); QColor black_color(Qt::black); black_color.setAlpha(50); QPen blackpen(black_color); blackpen.setWidth(3); rectangle = scene->addRect(-100, -100, 50, 50, blackpen, blueBrush); rectangle->setFlag(QGraphicsItem::ItemIsMovable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM