开发者

Qt Widget with Transparent Background

开发者 https://www.devze.com 2023-04-11 14:48 出处:网络
(I\'m using PySide, but I think the answer would be the same/similar for any language bindings). I\'m trying to take the shaped clock example, located here, and cause the face of the clock (circle) t

(I'm using PySide, but I think the answer would be the same/similar for any language bindings).

I'm trying to take the shaped clock example, located here, and cause the face of the clock (circle) to be transparent so that all I see are the clock hands and minute ticks. As is, when the example runs, it looks like this. I'm using Windows 7.

So far, I've tried the following (in the constructor):

  • self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
    • Clock appears (has presence in task bar), but I can't see it anywhere
  • self.setAttribute(QtCore.Qt.WA_NoSystemBa开发者_如何学Cckground)
    • Clock appears, but has solid black background.
  • self.setWindowOpacity(0.5)
    • Clock appears, but entire clock is transparent. I want the background (face) to be transparent, but I want the clock hands to be visible.


Got it!

This is from the original example code (constructor):

    ...
    self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
    ...

Here is the modified (and working per my question) version:

    ...
    self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
    ...

Removing the self.windowFlags() was the part I was missing. (I'm not exactly sure why I needed to remove this or why it was there to begin with... still much to learn).


If I remember correctly, you should have set its stylesheet also:

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setStyleSheet("background:transparent;")

Hope that helps.


in the clock example ther is :

void ShapedClock::resizeEvent(QResizeEvent * /* event */)
 {
     int side = qMin(width(), height());
     QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side,
                          side, QRegion::Ellipse);
     setMask(maskedRegion);
 }

the "setMask" do the round shape
but there's the same in PySide :

def resizeEvent(self, event):
        side = min(self.width(), self.height())
        maskedRegion = QtGui.QRegion(self.width()/2 - side/2, self.height()/2 - side/2, side, side, QtGui.QRegion.Ellipse)
        self.setMask(maskedRegion)

so it should work too ?

0

精彩评论

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

关注公众号