2010-01-06 92 views
1

當我點擊一個按鈕時,我的主窗口我希望它對鍵盤和鼠標事件變得透明,即所有鍵盤和鼠標事件都應該傳遞到它下面的任何窗口,就好像該窗口不是目前在那裏。鍵盤和鼠標事件透明部件

Qt::WA_TransparentForMouseEvents在這裏不起作用,因爲這隻會讓子窗口對鍵盤和鼠標事件透明,我猜。我的窗口是主窗口,我想將所有事件傳遞給桌面上的任何窗口,而不僅僅是父窗口。

+0

完全重複:http://stackoverflow.com/questions/987019/qt-top-level-widget-with-keyboard-and-mouse-event-transparency – 2010-01-06 09:09:48

回答

2

我一直在使用Qt::WA_TransparentForMouseEvents在我的應用程序,它的效果很好。

我不明白你面臨的問題,但它應該工作。如果您仍然有問題setattribute Qt::WA_TransparentForMouseEventsQt::WA_Translucentbackground

+0

具有u使用過 「WS_EX_TRANSPARENT」,在MFC ,我想要一些類似的東西。 這是什麼(Qt :: WA_TransparentForMouseEvents)使孩子對鼠標事件透明,並將所有的孩子事件發送到父,但如果我想使鼠標事件透明的窗口是頂層窗口或說是我的主窗口怎麼辦? 此標誌似乎並不適用於此。當我們使用setWindowOpacity(0.0)時,所有的事件都經過它,但問題是即使我在它上面繪製的東西也變成了不可見的(透明窗口),但是我希望它上面的圖形可見,並且仍然需要通過鼠標事件它。 – Gajender 2010-01-07 14:08:11

2

這裏是示例代碼,它使我能夠繪畫,而鼠標事件仍然通過它。

Sample::Sample(QWidget *pParent):QWidget(pParent) 
{ 
    setAttribute(Qt::WA_TranslucentBackground); 
    setAttribute(Qt::WA_TransparentForMouseEvents); 
    setWindowFlags(Qt::FramelessWindowHint); 
    QDesktopWidget qDesktopWidget; 
    QRect screenSize = qDesktopWidget.screenGeometry(); 
    setGeometry(screenSize); 
} 

Sample::~Sample() 
{ 
} 

void Sample::paintEvent(QPaintEvent*) 
{ 
    QDesktopWidget new QDesktopWidget(); 
    QRect rectangle = qDesktopWidget->screenGeometry(); 
    setGeometry(rectangle); 

    const QPoint points[5] = { 
     QPoint(0, 20), 
     QPoint(rectangle.width(), 20), 
     QPoint(rectangle.width(), rectangle.height()), 
     QPoint(0,rectangle.height()), 
     QPoint(0, 0) 
    }; 

    QPen pen(Qt::blue, 10, Qt::SolidLine, Qt::SquareCap); 
    QPainter painter(this); 
    painter.setPen(pen); 
    painter.drawPolyline(points, 5); 
    painter.end(); 
} 
+0

Rahul,即使我們沒有設置transparentForMouse標誌並且鼠標事件也會經過,但是當我們設置標誌「WindowStaysOnTopHint」透明度消失時,這裏做了什麼讓窗口變得半透明。還有一個問題就是它只是給透明的區域透明而不是滿的窗口,繪製區域還有鼠標。 這種行爲甚至可以通過在窗口小部件上使用SetMask()並使用alpha混合圖像來實現。 但我想要的是讓所有的事件都通過,鍵盤鼠標和all.drawing將永遠是用戶可見的,在我的情況下窗口也背景也。 – Gajender 2010-01-09 13:12:12