mousepress事件的问题
我刚刚问了类似的问题,但是(抱歉!)我想我需要更多帮助。我在 pyqt 中遇到信号问题。让我发布整个代码,它并不长,而且对我来说更容易解释...
from PyQt4 import QtGui, QtCore, Qt
import time
import math
class FenixGui(QtGui.QWidget):
def backgroundmousepressevent(self, event):
print "test 1"
self.offset = event.pos()
def backgroundmousemoveevent(self, event):
print "test 2"
x=event.globalX()
y=event.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x-x_w, y-y_w)
def __init__(self):
super(FenixGui, self).__init__()
# setting layout type
hboxlayout = QtGui.QHBoxLayout(self)
self.setLayout(hboxlayout)
# hiding title bar
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
# setting window size and position
self.setGeometry(200, 200, 862, 560)
self.setAttribute(Qt.Qt.WA_TranslucentBackground)
self.setAutoFillBackground(False)
# creating background window label
backgroundpixmap = QtGui.QPixmap("fenixbackground.png")
self.background = QtGui.QLabel(self)
self.background.setPixmap(backgroundpixmap)
self.background.setGeometry(0, 0, 862, 560)
# making window draggable by the window label
self.connect(self.background,QtCore.SIGNAL("mousePressEvent()"), self.backgroundmousepressevent)
self.connect(self.background,QtCore.SIGNAL("mouseMoveEvent()"), self.backgroundmousemoveevent)
# fenix logo
logopixmap = QtGui.QPixmap("fenixlogo.png")
self.logo = QtGui.QLabel(self)
self.logo.setPixmap(logopixmap)
self.logo.setGeometry(100, 100, 400, 150)
def main():
app = QtGui.QApplication([])
exm = FenixGui()
exm.show()
app.exec_()
if __name__ == '__main__':
main()
好吧,这就是代码,它只是一个简单的 gui,我想在屏幕上单击并拖动来使其可拖动背景中的任何地方。我的问题是:当我按下或移动按钮时,backgroundmousepressevent 和backgroundmousemoveevent 不会被触发。所以我想知道:错误在哪里?我是否拼写错误或什么?非常感谢!
马泰奥
I just asked a similar question but (sorry!) I think I'll need more help. I have a problem with signals in pyqt. Let me post the whole code, it isn't long and it is easier for me to explain...
from PyQt4 import QtGui, QtCore, Qt
import time
import math
class FenixGui(QtGui.QWidget):
def backgroundmousepressevent(self, event):
print "test 1"
self.offset = event.pos()
def backgroundmousemoveevent(self, event):
print "test 2"
x=event.globalX()
y=event.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x-x_w, y-y_w)
def __init__(self):
super(FenixGui, self).__init__()
# setting layout type
hboxlayout = QtGui.QHBoxLayout(self)
self.setLayout(hboxlayout)
# hiding title bar
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
# setting window size and position
self.setGeometry(200, 200, 862, 560)
self.setAttribute(Qt.Qt.WA_TranslucentBackground)
self.setAutoFillBackground(False)
# creating background window label
backgroundpixmap = QtGui.QPixmap("fenixbackground.png")
self.background = QtGui.QLabel(self)
self.background.setPixmap(backgroundpixmap)
self.background.setGeometry(0, 0, 862, 560)
# making window draggable by the window label
self.connect(self.background,QtCore.SIGNAL("mousePressEvent()"), self.backgroundmousepressevent)
self.connect(self.background,QtCore.SIGNAL("mouseMoveEvent()"), self.backgroundmousemoveevent)
# fenix logo
logopixmap = QtGui.QPixmap("fenixlogo.png")
self.logo = QtGui.QLabel(self)
self.logo.setPixmap(logopixmap)
self.logo.setGeometry(100, 100, 400, 150)
def main():
app = QtGui.QApplication([])
exm = FenixGui()
exm.show()
app.exec_()
if __name__ == '__main__':
main()
All right, so this is the code, it's just a simple gui that I wanted to make draggable around the screen clicking and dragging on any place in the background. My problem is: backgroundmousepressevent and backgroundmousemoveevent DON'T get fired when i press or move the button. So I wonder: where's the error? Did I mispell something or what? Thank you very much!
Matteo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Qt 中,事件不同于信号和槽。事件由
QEvent
对象表示,这些对象被传递到QObject
的event()
方法,在该方法中事件通常被分派给专门的方法,例如mousePressEvent
和mouseMoveEvent
。由于它们不是信号,因此您无法将它们连接到插槽。相反,只需重新实现事件函数来执行自定义操作。不过,请确保使用
super
调用原始实现,除非您知道自己在做什么。通常,当尝试连接到不存在的信号时,Qt 会通过向控制台写入警告消息来警告您。此外,您可以通过使用新式信号和槽来防止这种情况 而不是旧式的、更 C++ 风格的
SIGNAL()
函数:In Qt, events are different from signals and slots. Events are represented by
QEvent
objects that are passed to theevent()
method ofQObject
s, where they are typically dispatched to specialized methods such asmousePressEvent
andmouseMoveEvent
. Since they are not signals, you cannot connect them to slots.Instead, just re-implement event functions to do custom things. Make sure to call the original implementation with
super
, though, unless you know what you are doing.Typically, Qt will warn you when trying to connect to non-existing signals, by writing a warning message to the console. Additionally, you can prevent this situation by using new-style signals and slots instead of the old-style, more C++-ish
SIGNAL()
function:您正在尝试连接到 QWidget 的 mousePressEvent 和 mouseMoveEvent 信号,但它们并不作为信号存在。尝试重写这些方法。这对我有用:
You're trying to connect to QWidget's mousePressEvent and mouseMoveEvent signals, but they don't exist as signals. Try overriding the methods instead. This works for me: