You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt:替换已弃用的pos()方法——无框钢琴应用窗口拖动问题

解决Qt中pos()方法弃用的问题

针对你无框钢琴应用的窗口拖动代码,Qt6中QMouseEvent.pos()已被弃用,替换方案如下:

修改后的完整代码:

def mousePressEvent(self,e):
    modifiers = e.modifiers()
    if modifiers == Qt.ControlModifier:
        app.quit()
    else:
        self.mpos = e.position().toPoint()

def mouseMoveEvent(self,e):
    buttons = e.buttons()
    if buttons == Qt.LeftButton:
        dpos = e.position().toPoint() - self.mpos
        newpos = self.pos() + dpos
        self.move(newpos)

关键说明:

  • e.pos()替换为e.position().toPoint():Qt6中QMouseEvent.position()返回浮点型坐标(QPointF),调用toPoint()可转换为原代码使用的整数型QPoint,完全适配原有拖动逻辑。
  • 窗口的self.pos()方法并未被弃用,可以继续使用;如果后续版本出现弃用提示,可替换为self.frameGeometry().topLeft(),两者作用一致,均返回窗口左上角的屏幕坐标。

内容的提问来源于stack exchange,提问作者John Allsup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 01:18:25