gpt4 book ai didi

python - qwidget.setGeometry() 在 Linux 上的位置不正确

转载 作者:行者123 更新时间:2023-12-01 09:15:01 25 4
gpt4 key购买 nike

我正在尝试使用 qt 小部件覆盖 Maya 的事件 View 3d。

我有一个 Canvas 基类(一个透明的小部件),我可以在上面做一些绘画工作。它的父级是包装的 avtice 3d View 。一旦调整事件 3D View 的大小,就会发出 CanvasEventFilter.resize_event 信号以调整 Canvas 小部件的大小。

在 Mac 和 Windows 上一切正常。然而在 Linux 上,小部件是偏移的。我从 mapToGlobal() 获得的值似乎是正确的。然而 setGeometry() 并没有像我期望的那样更新小部件的位置和大小。

我在这里缺少什么?

from PySide2.QtGui import QPainter, QPen, QPainterPath
from PySide2.QtCore import Qt, QObject, Signal, Slot, QEvent, QPointF, QPoint
from PySide2.QtWidgets import QWidget, QLabel, QGridLayout
import maya.OpenMayaUI as omui
import shiboken2



class CanvasEventFilter(QObject):
resize_event = Signal(QEvent)

def __init__(self):
super(CanvasEventFilter, self).__init__()

def eventFilter(self, obj, event):
if event.type() == QEvent.Resize:
self.resize_event.emit(event)

return False

class Canvas(QWidget):
def __init__(self, parent):
super(Canvas, self).__init__(parent=parent)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.SplashScreen | Qt.WindowStaysOnTopHint | Qt.WindowTransparentForInput)
# self.setAttribute(Qt.WA_TranslucentBackground)
# self.setAttribute(Qt.WA_NoSystemBackground)
# self.setAttribute(Qt.WA_PaintOnScreen)
# self.setAttribute(Qt.WA_TransparentForMouseEvents)

self.canvas_event_filter = CanvasEventFilter()
self.install_event_filter()
self.resize()
self.show()

def install_event_filter(self):
self.canvas_event_filter.resize_event.connect(self.resize)
view_wdg = window_utils.active_view_wdg()
view_wdg.installEventFilter(self.canvas_event_filter)

def remove_event_filter(self):
view_wdg = active_view_wdg()
view_wdg.removeEventFilter(self.canvas_event_filter)

@Slot(QEvent)
def resize(self):
view_wdg = active_view_wdg()
wdg_size = view_wdg.rect()
wdg_pos = view_wdg.pos()
abs_pos = view_wdg.mapToGlobal(wdg_pos)
self.setGeometry(abs_pos.x(), abs_pos.y(), wdg_size.width(), wdg_size.height())

def __del__(self):
self.remove_event_filter()


def active_view_wdg():
view = omui.M3dView.active3dView()
active_view_widget = shiboken2.wrapInstance(long(view.widget()), QWidget)
return active_view_widget

Mac 上的黑色小部件与预期的位置完全一致。在 Linux 上它是偏移的

最佳答案

这似乎在 Centos 7 上非常适合我。当主窗口移动时,小部件会跟随,并在主窗口调整大小时正确调整大小。

我不得不重命名你的resize方法,因为这意外地覆盖了小部件的 QtWidgets.QWidget.resize方法(或者只需要使用 super 代替)。而不是setGeometry我可以简单地使用 QtWidgets.QWidget.resize 。我还必须删除 Qt.SplashScreen ,否则它无法正确跟随或调整大小:

from PySide2.QtGui import QPainter, QPen, QPainterPath
from PySide2.QtCore import Qt, QObject, Signal, Slot, QEvent, QPointF, QPoint, QSize
from PySide2.QtWidgets import QWidget, QLabel, QGridLayout
import maya.OpenMayaUI as omui
import shiboken2


class CanvasEventFilter(QObject):
resize_event = Signal(QEvent)

def __init__(self):
super(CanvasEventFilter, self).__init__()

def eventFilter(self, obj, event):
if event.type() == QEvent.Resize:
self.resize_event.emit(event)

return False


class Canvas(QWidget):
def __init__(self, parent):
super(Canvas, self).__init__(parent=parent)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowTransparentForInput)
self.setStyleSheet("QWidget {background-color:black;}")
self.canvas_event_filter = CanvasEventFilter()
self.install_event_filter()
self.resize_event()
self.show()

def install_event_filter(self):
self.canvas_event_filter.resize_event.connect(self.resize_event)
view_wdg = active_view_wdg()
view_wdg.installEventFilter(self.canvas_event_filter)

def remove_event_filter(self):
view_wdg = active_view_wdg()
view_wdg.removeEventFilter(self.canvas_event_filter)

@Slot(QEvent)
def resize_event(self):
view_wdg = active_view_wdg()
wdg_size = view_wdg.rect()
wdg_pos = view_wdg.pos()
abs_pos = view_wdg.mapToGlobal(wdg_pos)
self.resize(wdg_size.width(), wdg_size.height())

def __del__(self):
self.remove_event_filter()


def active_view_wdg():
view = omui.M3dView.active3dView()
active_view_widget = shiboken2.wrapInstance(long(view.widget()), QWidget)
return active_view_widget

tool = Canvas(active_view_wdg())
tool.show()

关于python - qwidget.setGeometry() 在 Linux 上的位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51346212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com