如何在if & 下调用mousePressEvent pyqt中的paintEvent还有什么?
在 PyQt5 中,我正在开发一个基于传感器的 GUI,其中我绘制了一个切换电源关闭/打开按钮,其中我想添加一个功能,其中我切换电源按钮,并且我的桌面 GUI 应在该切换上关闭。就像我们在 gui 的关闭 [X] 按钮中所做的那样。
这是toggle.py代码并在main.py代码上调用i
main.py
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# main dialog box
self.turning_Icon = None
self.setWindowTitle("Hatchery System")
self.setStyleSheet("background-color: #2c313c;")
self.setFixedWidth(1400)
self.setFixedHeight(950)
self.setWindowFlags(Qt.FramelessWindowHint)
# Create Container and Layout
self.container = QFrame(self)
self.container.move(100, 50)
self.container.resize(100, 50)
self.container.setStyleSheet("background-color: #2c313c;")
self.container.layout = QVBoxLayout()
# toggle_power_button
self.toggle = PyToggle()
self.toggle.setStyleSheet("background-color: white")
self.toggle.move(50, 50)
self.container.layout.addWidget(self.toggle, Qt.AlignCenter, Qt.AlignCenter)
self.container.setLayout(self.container.layout)
toggle.py代码:
import pylab as p
从 PyQt5.QtCore 导入 *
从 PyQt5.QtGui 导入 *
from PyQt5.QtWidgets import *
class PyToggle(QCheckBox):
def __init__(
self,
width=60,
# height=50,
bg_color="#777",
circle_color="#fff",
active_color="#00BCff"
# active_color="red"
):
QCheckBox.__init__(self)
self.setFixedSize(width, 28)
# self.setFixedSize(height, 40)
self.setCursor(Qt.PointingHandCursor)
# colors
self._bg_color = bg_color
self._circle_color = circle_color
self._active_color = active_color
# connect state changed
self.stateChanged.connect(self.debug)
def debug(self):
print(f"status: {self.isChecked()}")
def hitButton(self, pos: QPoint):
return self.contentsRect().contains(pos)
def paintEvent(self, e):
# SET painter
p = QPainter(self)
p.setRenderHint(QPainter.Antialiasing)
p.setPen(Qt.NoPen)
rect = QRect(0, 0, self.width(), self.height())
p.setBrush(QColor(self._bg_color))
p.drawRoundedRect(0, 0, rect.width(), self.height(), self.height() / 2, self.height() / 2)
p.end()
def paintEvent(self, e):
# SET painter
p = QPainter(self)
p.setRenderHint(QPainter.Antialiasing)
# SET as No PEN
p.setPen(Qt.NoPen)
# draw rect
rect = QRect(0, 0, self.width(), self.height())
if not self.isChecked():
# draw BG
p.setBrush(QColor(self._bg_color))
p.drawRoundedRect(0, 0, rect.width(), self.height(), self.height()/2, self.height()/2)
p.setBrush(QColor(self._circle_color))
p.drawEllipse(3, 3, 22, 22)
p.mousePressEvent = self.clickLine
else:
p.setBrush(QColor(self._active_color))
p.drawRoundedRect(0, 0, rect.width(), self.height(), self.height() / 2, self.height() / 2)
p.setBrush(QColor(self._circle_color))
p.drawEllipse(self.width() - 26, 3, 22, 22)
def clickLine(self, mouseEvent):
p.clicked.connect(self.close)
这里在 if 条件下我调用 mousePressEvent 但它不起作用
In PyQt5, I am developing a sensors based GUI where i draw a toggle power off/on button in which i want to add a functionality where i toggle the power button and my desktop gui should be closed on that toggle. like we do in close [X] button of the gui.
here is the toggle.py code and call i on my main.py code
main.py
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# main dialog box
self.turning_Icon = None
self.setWindowTitle("Hatchery System")
self.setStyleSheet("background-color: #2c313c;")
self.setFixedWidth(1400)
self.setFixedHeight(950)
self.setWindowFlags(Qt.FramelessWindowHint)
# Create Container and Layout
self.container = QFrame(self)
self.container.move(100, 50)
self.container.resize(100, 50)
self.container.setStyleSheet("background-color: #2c313c;")
self.container.layout = QVBoxLayout()
# toggle_power_button
self.toggle = PyToggle()
self.toggle.setStyleSheet("background-color: white")
self.toggle.move(50, 50)
self.container.layout.addWidget(self.toggle, Qt.AlignCenter, Qt.AlignCenter)
self.container.setLayout(self.container.layout)
toggle.py code:
import pylab as p
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class PyToggle(QCheckBox):
def __init__(
self,
width=60,
# height=50,
bg_color="#777",
circle_color="#fff",
active_color="#00BCff"
# active_color="red"
):
QCheckBox.__init__(self)
self.setFixedSize(width, 28)
# self.setFixedSize(height, 40)
self.setCursor(Qt.PointingHandCursor)
# colors
self._bg_color = bg_color
self._circle_color = circle_color
self._active_color = active_color
# connect state changed
self.stateChanged.connect(self.debug)
def debug(self):
print(f"status: {self.isChecked()}")
def hitButton(self, pos: QPoint):
return self.contentsRect().contains(pos)
def paintEvent(self, e):
# SET painter
p = QPainter(self)
p.setRenderHint(QPainter.Antialiasing)
p.setPen(Qt.NoPen)
rect = QRect(0, 0, self.width(), self.height())
p.setBrush(QColor(self._bg_color))
p.drawRoundedRect(0, 0, rect.width(), self.height(), self.height() / 2, self.height() / 2)
p.end()
def paintEvent(self, e):
# SET painter
p = QPainter(self)
p.setRenderHint(QPainter.Antialiasing)
# SET as No PEN
p.setPen(Qt.NoPen)
# draw rect
rect = QRect(0, 0, self.width(), self.height())
if not self.isChecked():
# draw BG
p.setBrush(QColor(self._bg_color))
p.drawRoundedRect(0, 0, rect.width(), self.height(), self.height()/2, self.height()/2)
p.setBrush(QColor(self._circle_color))
p.drawEllipse(3, 3, 22, 22)
p.mousePressEvent = self.clickLine
else:
p.setBrush(QColor(self._active_color))
p.drawRoundedRect(0, 0, rect.width(), self.height(), self.height() / 2, self.height() / 2)
p.setBrush(QColor(self._circle_color))
p.drawEllipse(self.width() - 26, 3, 22, 22)
def clickLine(self, mouseEvent):
p.clicked.connect(self.close)
here in if condition i call mousePressEvent but its not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于要求仅在再次取消选中该复选框时才关闭窗口,因此解决方案是连接到仅在这种情况下关闭的函数。请注意,您不应将
stateChanged
信号用于双状态复选框,因为它返回Qt.CheckState
枚举,有 3 个状态。请改用toggled
信号。关于您的尝试的重要说明。
首先,绘图函数永远不应该尝试做与绘图不直接相关的事情。这很重要,因为这些函数被频繁调用,可能每秒调用数十次,在某些情况下甚至每次将鼠标移到小部件上方时。
最重要的是,您覆盖的方法只是连接了 clicked 信号,这是错误的,原因有两个:
另一个需要记住的重要方面是 PyQt 对函数使用引用缓存;第一次使用默认实现调用虚拟函数后,覆盖实例属性将不起作用。
假设您想做相反的事情(当状态为 True 时连接信号),它不会起作用,因为那时默认的 mousePressEvent 处理程序已经是调用,覆盖根本没有任何效果。
Since the requirement is to close the window only when the check box is unchecked again, the solution is to connect to a function that will only close in that case. Note that you should not use the
stateChanged
signal for dual state check boxes, as it returns aQt.CheckState
enum, which has 3 states. Use thetoggled
signal instead.An important note about your attempt.
First of all, a paint function should never try to do something that is not directly related to drawing. This is important as those functions are called very often, potentially dozens of times each second, and in some cases even every time the mouse is moved above the widget.
Most importantly, your overwritten method just connects the
clicked
signal, which is wrong for two reasons:clicked
signal), you're preventing all of that - and the signal will never be emitted because it never gets checked/unchecked;Another important aspect to remember is that PyQt uses reference caching for functions; after the first time a virtual function is called with the default implementation, overwriting the instance attribute will have no effect.
Supposing you wanted to do the opposite (connect the signal when the state is
True
), it wouldn't have worked because at that time the defaultmousePressEvent
handler would have been already called, and the overwriting would have had no effect at all.