簡體   English   中英

QQuickView(QML)透明用於鼠標事件

[英]QQuickView (QML) transparent for mouse events

我有一個按鈕居中的大矩形。 我希望我的矩形對鼠標事件是透明的,除了按鈕,它必須是可點擊的。 我的意思是,我希望能夠用鼠標選擇矩形下的代碼,就像沒有顯示矩形一樣。

我為所有大型Rect添加了一個MouseArea,試圖忽略鼠標事件,但它不起作用。

我讀到'Qt :: WA_TransparentForMouseEvents'用於此目的,但在Qt窗口中我認為很簡單,在我的情況下不可用。

提前致謝

我的QML是從main.cpp加載的:

   QQuickView* pView = new QQuickView();

    pView->setSource(QUrl("qrc:/MyRect.qml"));
    pView->setFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    pView->setColor("transparent");
    pView->show();

MyRect.qml:

import QtQuick 2.0
import QtQuick.Controls 1.4

Rectangle {
    width: 500
    height: 500

    color: "green" // it would be transparent
    opacity: 0.5

    Button {
        anchors.centerIn: parent
        height: 50; width: 50
        onClicked: console.log("clicked");
    }

    MouseArea {
        anchors.fill: parent
        enabled: false
        propagateComposedEvents: true
        hoverEnabled: false

        // All this code I think is useless...
        onClicked: mouse.accepted = false
        onReleased: mouse.accepted = false
        onEntered: mouse.accepted = false
        onExited:  mouse.accepted = false
        onWheel:  mouse.accepted = false
    }
}

默認情況下, Rectangle對鼠標單擊是透明的。 如果你拿走那個MouseAreaButton會收到點擊,而Rectangle上的所有點擊都會傳遞給它的封閉父級:

import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick 2.7
import QtQuick.Controls 1.5
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3


ApplicationWindow {
    width: 200; height: 150; visible: true
    property string status;

    ColumnLayout {
      Rectangle {
        width:100;height:100;

        MouseArea {
            anchors.fill: parent
            onClicked: status = "Enclosing Rectangle Clicked";    
        }

        Rectangle {
            width: 75
            height: 75
            color: "green" // it would be transparent
            opacity: 0.5
            Button {
                anchors.centerIn: parent
                height: 25; width: 25
                onClicked: status = "Button clicked";
            }
        }
      }
      Text{ text: status}
    }
}

在行動:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM