I would like to know how I can change the size of a qframe when the cursor is over and outside of the
that is to say:
default: width (50) cursor on the frame: width (100) cursor outside the frame: width (50)
from what I've researched I think it's using dragEnterEvent and dragLeaveEvent
this is what I tried:
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import QtCore
from PyQt5 import uic
class Principal(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
uic.loadUi("other.ui",self)
def dragEnterEvent(self,obj,event):
if event == self.mouseMoveEvent:
if obj == self.frame:
self.frame.width(100)
def dragLeaveEvent(self,obj,event):
if event == self.mouseMoveEvent:
if obj == self.frame:
self.frame.width(50)
app = QApplication([])
p = Principal()
p.show()
app.exec_()
other.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>580</width>
<height>368</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>180</x>
<y>70</y>
<width>151</width>
<height>141</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">
background:red;
</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
dragEnterEvent and dragLeaveEvent are called when the mouse is dragged, not when the mouse enters or exits but also takes as a reference the class where it is written, in this case the window and not the frame, therefore it is inappropriate.
If you want to be notified by events of objects that can not be overwritten as the frame, install an event filter using the events of type QEvent::Enter
and QEvent::Leave
.
from PyQt5 import QtCore, QtWidgets, uic
class Principal(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Principal, self).__init__(parent)
uic.loadUi("other.ui",self)
self.frame.installEventFilter(self)
def eventFilter(self, watched, event):
if self.frame is watched:
if event.type() == QtCore.QEvent.Enter:
self.frame.resize(50, self.frame.height())
elif event.type() == QtCore.QEvent.Leave:
self.frame.resize(100, self.frame.height())
return super(Principal, self).eventFilter(watched, event)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
p = Principal()
p.show()
sys.exit(app.exec_())
On the other hand as an observation in its logic to an unstable region:
Consider that initially the position of the mouse is outside the frame.
After the mouse is placed on the right side in position 90 with respect to the frame, then with its logic is within the frame so it must change its width to 50.
With the change the mouse will be outside so the width will change to 100 being inside the frame and we would go back to step 2, generating an oscillation of the width.