开发者

QRubberBand like feature - Static selection area

开发者 https://www.devze.com 2023-02-01 08:10 出处:网络
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

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.


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;

};


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消