python-3.x - 打开 QComboBox 会触发 mouseMoveEvent 但不会触发 mousePressEvent
问题描述
在我的代码中,我有两个组合框,我故意设计了一个无框无边框窗口;所以我必须手动定义鼠标事件以在点击和拖动时移动窗口,并在边缘调整大小。没有组合框它可以完美地工作,但是以某种方式单击组合打开它们只会触发mouseMoveEvent而不是mousePressEvent,从而导致关于没有self.old_Pos的错误。如果我们取消注释最后三行init函数,这个错误就消失了,但是在打开组合时整个窗口将被移位。我该如何克服这个问题?
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QWidget.__init__(self)
self.combo1 = QComboBox()
for i in range (0,10):
self.combo1.addItem('Combo1 label %s' %str(i))
self.combo2 = QComboBox()
for i in range (0,15):
self.combo2.addItem('Combo1 label %s' %str(i))
main_layout = QVBoxLayout()
layout = QHBoxLayout()
layout.addWidget(self.combo1)
layout.addWidget(self.combo2)
# design title bar
title_bar = QHBoxLayout()
title_bar.setObjectName('HeaderBar')
title_bar.setContentsMargins(0,0,0,0)
title = QLabel('title bar')
btn_size = 35
btn_close = QPushButton("x")
btn_close.clicked.connect(self.btn_close_clicked)
btn_close.setFixedSize(btn_size,btn_size)
btn_close.setStyleSheet("background-color: red;")
btn_min = QPushButton("_")
btn_min.clicked.connect(self.btn_min_clicked)
btn_min.setFixedSize(btn_size, btn_size)
btn_min.setStyleSheet("background-color: gray;")
self.btn_max = QPushButton("+")
self.btn_max.clicked.connect(self.btn_max_clicked)
self.btn_max.setFixedSize(btn_size, btn_size)
self.btn_max.setStyleSheet("background-color: gray;")
title.setAlignment(Qt.AlignCenter)
title_bar.addWidget(title)
title_bar.addWidget(btn_min)
title_bar.addWidget(self.btn_max)
title_bar.addWidget(btn_close)
main_layout.addLayout(title_bar)
main_layout.addLayout(layout)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setLayout(main_layout)
#self.old_Pos = QPoint(0,0)
#self.old_width = self.width()
#self.old_height = self.height()
def mousePressEvent(self, event):
self.old_Pos = event.globalPos()
self.old_width = self.width()
self.old_height = self.height()
def mouseMoveEvent(self, event):
delta = QPoint (event.globalPos() - self.old_Pos)
if (self.old_Pos.x() > self.x() + self.old_width - 10) or (self.old_Pos.y() > self.y() + self.old_height - 10):
self.setFixedSize(self.old_width + delta.x(),self.old_height + delta.y())
else:
self.move(self.x() + delta.x(), self.y() + delta.y())
self.old_Pos = event.globalPos()
def btn_close_clicked(self):
quit()
def btn_max_clicked(self):
if self.isMaximized():
self.showNormal()
self.btn_max.setText('+')
else:
self.showMaximized()
self.btn_max.setText('R')
def btn_min_clicked(self):
self.showMinimized()
app = QApplication([])
mainapp = MainWindow()
mainapp.show()
app.exec_()
解决方案
我相信我解决了这个问题。我们所要做的就是在andself.old_Pos = None
内部定义,并在 mouseMoveEvent 中放置一个语句。这是代码:__init__
mouseReleaseEvent
if self.old_Pos:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QWidget.__init__(self)
self.combo1 = QComboBox()
for i in range (0,10):
self.combo1.addItem('Combo1 label %s' %str(i))
self.combo2 = QComboBox()
for i in range (0,15):
self.combo2.addItem('Combo2 label %s' %str(i))
main_layout = QVBoxLayout()
layout = QHBoxLayout()
layout.addWidget(self.combo1)
layout.addWidget(self.combo2)
# design title bar
title_bar = QHBoxLayout()
title_bar.setObjectName('HeaderBar')
title_bar.setContentsMargins(0,0,0,0)
title = QLabel('title bar')
btn_size = 35
btn_close = QPushButton("x")
btn_close.clicked.connect(self.btn_close_clicked)
btn_close.setFixedSize(btn_size,btn_size)
btn_close.setStyleSheet("background-color: red;")
btn_min = QPushButton("_")
btn_min.clicked.connect(self.btn_min_clicked)
btn_min.setFixedSize(btn_size, btn_size)
btn_min.setStyleSheet("background-color: gray;")
self.btn_max = QPushButton("+")
self.btn_max.clicked.connect(self.btn_max_clicked)
self.btn_max.setFixedSize(btn_size, btn_size)
self.btn_max.setStyleSheet("background-color: gray;")
title.setAlignment(Qt.AlignCenter)
title_bar.addWidget(title)
title_bar.addWidget(btn_min)
title_bar.addWidget(self.btn_max)
title_bar.addWidget(btn_close)
main_layout.addLayout(title_bar)
main_layout.addLayout(layout)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setLayout(main_layout)
self.work_with_combo = True
self.old_Pos = None
def mousePressEvent(self, event):
self.old_Pos = event.globalPos()
self.old_width = self.width()
self.old_height = self.height()
def mouseMoveEvent(self, event):
if self.old_Pos:
delta = QPoint (event.globalPos() - self.old_Pos)
if (self.old_Pos.x() > self.x() + self.old_width - 10) or (self.old_Pos.y() > self.y() + self.old_height - 10):
self.setFixedSize(self.old_width + delta.x(),self.old_height + delta.y())
else:
self.move(self.x() + delta.x(), self.y() + delta.y())
self.old_Pos = event.globalPos()
def mouseReleaseEvent(self, event):
self.old_Pos = None
def btn_close_clicked(self):
quit()
def btn_max_clicked(self):
if self.isMaximized():
self.showNormal()
self.btn_max.setText('+')
else:
self.showMaximized()
self.btn_max.setText('R')
def btn_min_clicked(self):
self.showMinimized()
app = QApplication([])
mainapp = MainWindow()
mainapp.show()
app.exec_()
推荐阅读
- c# - MVVM 子类属性绑定
- symfony - 如何从 Doctrine 实体(Symfony 4)加载表单选项?
- c# - 当按下 Button1 的边缘时,MessageBox 有时不会打开
- intellij-idea - 如何使用 super dev (intellij) 调试 gwt
- python - 派生类的类/元类方法装饰器
- python - 基于值Python DataFrame添加计数器
- python - 对熊猫数据框进行子集化并保留原始大小
- hdfs - Hadoop/HDFS 3.1.1(在 Java 11 上)加载文件资源管理器时 Web UI 崩溃?
- azure-cosmosdb - Cosmos DB - 停止 IChangeFeedProcessor 实例
- java - java.io.IOException:连接被主机中的软件中止,在 selenium java 中使用 Sendkey()