QRubberBand 类似功能 - 静态选择区域
我试图在桌面/打开的窗口顶部绘制一个选择区域,使用 QRubberBand 效果很好,但由于它没有样式表命令,我该如何更改边框的颜色和宽度并使其内部完全透明?
编辑: Qt 中是否有与 QRubberBand 类似的使用方法?更改绘图方法会带来很多问题(左侧和顶部边框比右侧和底部大 1 个像素,标记区域似乎无法完全透明)。
Edit2:它将覆盖的区域是静态的,而不是由用户拖动的区域。
Edit3:
class CustomRubberBand : public QRubberBand
{
public:
CustomRubberBand(Shape s, QWidget * p = 0) : QRubberBand(s, p)
{
}
protected:
void paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);
QPainter painter;
QPen pen(Qt::red, 6);
pen.setStyle(Qt::SolidLine);
painter.begin(this);
painter.setPen(pen);
painter.drawRect(pe->rect());
painter.end();
}
};
这给了我我想要的边框,但我还没有找到任何关于删除背景(完全透明)的有效方法...似乎 Vista 和 Qt 存在问题。
关于如何删除背景有什么建议吗?目前没有绘画方法,它是半透明的白色背景,而不是默认的蓝色背景。
编辑4: 这显示了问题:可见背景错误注意带有边框的背景如何半透明的白色。我使用的绘画方法不会绘制它,而只会绘制边框。我希望它完全不可见,并且设置对象的不透明度也会使边框透明,但它不应该是这样。
I'm trying to draw a selection area on top of the desktop/opened windows, which works well by using QRubberBand
, but seeing as it does not have a stylesheet command, how would I be able to change the color and width of the border and making the inside of it completely transparent?
Edit: Is there a similar method to use than QRubberBand in Qt? Changing the painter methods gives a lot of problems (border is one pixel larger on the left and top than right and bottom, the marked area seems not to be able to be completely transparent).
Edit2: The area it will cover is static, not something that is dragged by the user.
Edit3:
class CustomRubberBand : public QRubberBand
{
public:
CustomRubberBand(Shape s, QWidget * p = 0) : QRubberBand(s, p)
{
}
protected:
void paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);
QPainter painter;
QPen pen(Qt::red, 6);
pen.setStyle(Qt::SolidLine);
painter.begin(this);
painter.setPen(pen);
painter.drawRect(pe->rect());
painter.end();
}
};
This gives me the border around it that I want, but I haven't found anything about removing the background (completely transparent) that works... Seems like there is a problem with Vista and Qt with this.
Any tips on how to remove the background? Right now with no painting method for it it is a semi-transparent white background instead of the default blue one.
Edit4:
This shows the problem: Visible background error notice how the background, with the border, is a semi transparent white. The paint method I'm using does not draw this but only the border. I want it to be completely invisible, and setting the opacity for the object will also make the border transparent, which it should not be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在paintEvent函数中使用透明的QPalette来实现您想要做的事情。
You can use a transparent QPalette in the paintEvent function to achieve what you are trying to do.
QRubberBand
继承自QWidget
,后者又支持setStyleSheet
功能,请参阅QRubberBand 成员函数
列表。如果这不适合您,只需覆盖
::paintEvent
,请参阅此示例
。QRubberBand
inherits fromQWidget
which in turn supportssetStyleSheet
function, see theQRubberBand member functions
listing.If this is not working right for you just override
::paintEvent
, see thisexample
.