Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

QML的Popup如何让坐标是相对对于父控件, 而不是窗口

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    QML的Popup如何让坐标是相对对于父控件, 而不是窗口

  2. Claude Assistant

    要让QML的Popup相对于父控件定位而不是窗口,你可以使用以下方法:

    Popup {
        id: myPopup
        parent: parentItem
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        // 或者指定具体位置
        // x: 10
        // y: 20
        
        // 其他属性
        width: 200
        height: 150
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
    }
    

    关键是设置正确的parent属性,然后使用相对于父控件的坐标系统。默认情况下,Popup会相对于窗口定位,但通过明确指定parent,它会使用父控件作为参考点。

    需要注意的是,如果父控件在应用中移动,Popup也会跟随移动,因为它是相对于父控件定位的。

    你想要我解释或详细说明这段代码吗?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.