QRubberBand 类似功能 - 静态选择区域

发布于 2024-10-09 06:01:57 字数 1011 浏览 2 评论 0原文

我试图在桌面/打开的窗口顶部绘制一个选择区域,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聆听风音 2024-10-16 06:01:57

您可以在paintEvent函数中使用透明的QPalette来实现您想要做的事情。

class ScreenViewport : public QRubberBand
{
Q_OBJECT
public:
ScreenViewport(Shape shape, QWidget *parent = 0) : QRubberBand(shape,parent){
}
protected:
void paintEvent(QPaintEvent *pe){

    pal = new QPalette(Qt::transparent);
    setPalette(*pal);

    Q_UNUSED(pe);

    QPainter painter;
    QPen pen(Qt::red, 6);
    pen.setStyle(Qt::DashLine);

    painter.begin(this);
    painter.setPen(pen);
    painter.drawRect(pe->rect());
    painter.end();
}

private:
QPalette *pal;

};

You can use a transparent QPalette in the paintEvent function to achieve what you are trying to do.

class ScreenViewport : public QRubberBand
{
Q_OBJECT
public:
ScreenViewport(Shape shape, QWidget *parent = 0) : QRubberBand(shape,parent){
}
protected:
void paintEvent(QPaintEvent *pe){

    pal = new QPalette(Qt::transparent);
    setPalette(*pal);

    Q_UNUSED(pe);

    QPainter painter;
    QPen pen(Qt::red, 6);
    pen.setStyle(Qt::DashLine);

    painter.begin(this);
    painter.setPen(pen);
    painter.drawRect(pe->rect());
    painter.end();
}

private:
QPalette *pal;

};
作业与我同在 2024-10-16 06:01:57

QRubberBand继承自QWidget,后者又支持setStyleSheet功能,请参阅QRubberBand 成员函数 列表。

如果这不适合您,只需覆盖 ::paintEvent ,请参阅此 示例

QRubberBand inherits from QWidget which in turn supports setStyleSheet function, see the QRubberBand member functions listing.

If this is not working right for you just override ::paintEvent , see this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文