1

I have the following window with frames.

I want frame to be highlighted (in my case change its shape) when mouse is in its area.

from PyQt4 import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()
window_layout = QtGui.QVBoxLayout()
window.setLayout(window_layout)

#fill content
for i in range(10):
    label = QtGui.QLabel(str(i))
    frame = QtGui.QFrame()
    frame_layout = QtGui.QVBoxLayout()
    frame.setLayout(frame_layout)
    frame_layout.addWidget(label)
    window_layout.addWidget(frame)

def layout_widgets(layout):
   return (layout.itemAt(i) for i in range(layout.count()))

def mouse_enter(event):
    print 'frame enter'
    w.widget().setFrameShape(3)

def mouse_leave(event):
    print 'frame leave'
    w.widget().setFrameShape(0)

for w in layout_widgets(window_layout):
    print w.widget()

    w.widget().enterEvent = mouse_enter
    w.widget().leaveEvent = mouse_leave

window.show()
sys.exit(app.exec_())

It works but only the last frame in layout highlights. How to make only that frame change its shape where the mouse is?

I've tried the following:

def mouse_enter(event, frame):
    print 'frame enter'
    frame.setFrameShape(3)

w.widget().enterEvent = functools.partial(mouse_enter, w.widget()) 

but it gives an error. I have found one more way to do that - signal mapper but I have no idea how to use it.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
eodj
  • 25
  • 5

1 Answers1

0

The problem in your code the variable w when executing the for is left with the last element, so it will only be executed in the latter. To solve this I have implemented a Frame class that inherits from QFrame where I overwrite the enterEvent and leaveEvent functions.

from PyQt4 import QtGui, QtCore
import sys


class Frame(QtGui.QFrame):
    def __init__(self, text, parent=None):
        super(Frame, self).__init__(parent=parent)
        label = QtGui.QLabel(text)
        frame_layout = QtGui.QVBoxLayout()
        frame_layout.addWidget(label)
        self.setLayout(frame_layout)

    def enterEvent(self, event):
        self.setFrameShape(3)

    def leaveEvent(self, event):
        self.setFrameShape(0)

app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()
window_layout = QtGui.QVBoxLayout()
window.setLayout(window_layout)

for i in range(10):
    frame = Frame(str(i))
    window_layout.addWidget(frame)

window.show()
sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241