小部件不显示macOS活力(NSVisualEffectView)

时间:2018-07-21 03:59:28

标签: python python-3.x pyqt5 appkit pyobjc

我目前有一个非常简单的PyQt5应用程序(只需一个按钮)。我希望它具有活力,因此我已将ObjC活力代码移植到Python。我的活力代码如下:

frame = NSMakeRect(0, 0, * self.get_screen_size())  # get_screen_size returns the resolution of the monitor
view = objc.objc_object(c_void_p=self.winId().__int__())  # returns NSView of current window

visualEffectView = NSVisualEffectView.new()
visualEffectView.setAutoresizingMask_(NSViewWidthSizable|NSViewHeightSizable)  # equivalent to: visualEffectView.autoresizingMask = NSViewW...
visualEffectView.setFrame_(frame)
visualEffectView.setState_(NSVisualEffectStateActive)
visualEffectView.setMaterial_(NSVisualEffectMaterialDark)
visualEffectView.setBlendingMode_(NSVisualEffectBlendingModeBehindWindow)

window = view.window()
window.contentView().addSubview_positioned_relativeTo_(visualEffectView, NSWindowBelow, None)  
    # equal to: [window.contentView addSubview:visualEffectView positioned:NSWindowBelow relativeTo:nul]

window.setTitlebarAppearsTransparent_(True)
window.setStyleMask_(window.styleMask() | NSFullSizeContentViewWindowMask)  # so the title bar is also vibrant

self.repaint()

我要做的是绘制一个按钮:btn = QPushButton("test", self)

self是从QMainWindow继承的类,其他所有内容都不重要。

带有window.contentView().addSubview...行的行为已被注释掉(没有振动)

没有注释的行为(充满活力)

谢谢!

1 个答案:

答案 0 :(得分:0)

实际上绘制了UI元素,但是NSVisualEffectView覆盖了它们。 我通过向窗口添加另一个视图QMacCocoaViewContainer解决了该问题。

您还需要将Window设置为透明,否则小部件将具有轻微的背景边框。

如果您使用充满活力的深色窗户,请确保正确设置外观,以便正确呈现按钮,标签等。

frame = NSMakeRect(0, 0, self.width(), self.height())
view = objc.objc_object(c_void_p=self.winId().__int__())

visualEffectView = NSVisualEffectView.new()
visualEffectView.setAutoresizingMask_(NSViewWidthSizable|NSViewHeightSizable)
visualEffectView.setWantsLayer_(True)
visualEffectView.setFrame_(frame)
visualEffectView.setState_(NSVisualEffectStateActive)
visualEffectView.setMaterial_(NSVisualEffectMaterialUltraDark)
visualEffectView.setBlendingMode_(NSVisualEffectBlendingModeBehindWindow)
visualEffectView.setWantsLayer_(True)

self.setAttribute(Qt.WA_TranslucentBackground, True)

window = view.window()
content = window.contentView()

container = QMacCocoaViewContainer(0, self)
content.addSubview_positioned_relativeTo_(visualEffectView, NSWindowBelow, container)  

window.setTitlebarAppearsTransparent_(True)
window.setStyleMask_(window.styleMask() | NSFullSizeContentViewWindowMask)

appearance = NSAppearance.appearanceNamed_('NSAppearanceNameVibrantDark')
window.setAppearance_(appearance)

结果:

enter image description here