View Full Version : Make QMainWindow transparent, but still respond to mouseEvents

5th November 2011, 17:50

I have a program consisting of many drawn rectangles on a QMainWindow with FramelessWindowHint. To still be able to move the window I overrode mouseMove- and mousePressEvent. Next I made the window transparent and as a result could only move it with the mouse over one of the rectangles. What I want is to be able to move it when the mouse is over the (transparent!) MainWindow. Should I use another method to make it transparent or is there any way I can make it respond to my mouseEvents?

Here's a simplified example code (I'm using PyQt 4.7 with Python 2.6.6 on Windows 7):

import sys
from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QMainWindow):

def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
#self.setAttribute(QtCore.Qt.WA_TranslucentBackgro und)

def mousePressEvent(self, event):
if (event.button() == QtCore.Qt.LeftButton):
self.drag_position = event.globalPos() - self.pos();

def mouseMoveEvent(self, event):
if (event.buttons() == QtCore.Qt.LeftButton):
self.move(event.globalPos().x() - self.drag_position.x(),
event.globalPos().y() - self.drag_position.y());

def initUI(self):
# do some drawing here

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)

window = MyWindow()