I'm trying to implement an event in PyQT5, but i get this error:
TypeError: installEventFilter(self, QObject): argument 1 has unexpected type 'MainWindow_EXEC'
This is my code
import sys
from time import sleep
from PyQt5 import QtCore, QtWidgets
from view_cortes2 import Ui_cortes2enter
class MainWindow_EXEC():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
cortes2 = QtWidgets.QMainWindow()
self.ui = Ui_cortes2()
self.ui.setupUi(cortes2)
self.flag = 0
self.ui.ledit_corteA.installEventFilter(self)
self.ui.ledit_corteB.installEventFilter(self)
self.ui.buttonGroup.buttonClicked.connect(self.handleButtons)
cortes2.show()
sys.exit(app.exec_())
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.FocusIn and source is self.ui.ledit_corteA):
print("A")
self.flag = 0
if (event.type() == QtCore.QEvent.FocusIn and source is self.ui.ledit_corteA):
print("B")
self.flag = 1
return super(cortes2, self).eventFilter(source, event)
if __name__ == "__main__":
MainWindow_EXEC()
The event that I'm trying to add is when I focus in a TextEdit it changes the value of a flag. If i change
self.ui.ledit_corteA.installEventFilter(self)
by
self.ui.ledit_corteA.installEventFilter(cortes2)
I works, but never changes the value of my flag.
Please help.
installEventFilter expects a QObject, and in your case MainWindow_EXEC is not.
If you are using the Qt Designer design it is recommended to create a new class that inherits from the appropriate widget and use the class provided by Qt Designer to fill it as shown below:
import sys
from PyQt5 import QtCore, QtWidgets
from view_cortes2 import Ui_cortes2
class MainWindow(QtWidgets.QMainWindow, Ui_cortes2):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.flag = 0
self.ledit_corteA.installEventFilter(self)
self.ledit_corteB.installEventFilter(self)
#self.buttonGroup.buttonClicked.connect(self.handleButtons)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.FocusIn and source is self.ledit_corteA:
print("A")
self.flag = 0
if event.type() == QtCore.QEvent.FocusIn and source is self.ledit_corteB:
print("B")
self.flag = 1
return super(MainWindow, self).eventFilter(source, event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
References:
http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
Related
Hello I have a QWidget and If I click on it, I want to get the object (the QWidget Element I clicked on) is there anyway to do that?
I already found some code but I only get the MouseClickEvent from
self.widget_34.mouseReleaseEvent = lambda event: self.myfunction(event)
Although the solution offered by #Cin is interesting, it has a serious problem: it cancels the mousePressEvent of the widget, so the widget loses the behavior it could have when the widget is pressed, for example the button no longer emits the clicked signal, other widget also They will have the same problem.
A less intrusive solution is to use eventFilter:
import sys
import weakref
from PyQt5 import QtCore, QtWidgets
class ClickListener(QtCore.QObject):
clicked = QtCore.pyqtSignal(QtWidgets.QWidget)
def addWidget(self, widget, other_widget=None):
if not hasattr(self, "_widgets"):
self._widgets = {}
widget.installEventFilter(self)
self._widgets[widget] = widget if other_widget is None else other_widget
weakref.ref(widget, self.removeWidget)
def eventFilter(self, obj, event):
if (
obj in self._widgets
and event.type() == QtCore.QEvent.MouseButtonPress
):
self.clicked.emit(self._widgets[obj])
return super(ClickListener, self).eventFilter(obj, event)
def removeWidget(self, widget):
if hasattr(self, "_widgets"):
if widget in self._widgets:
del self._widgets[widget]
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
button = QtWidgets.QPushButton("Press Me")
label = QtWidgets.QLabel("Stack Overflow")
spinBox = QtWidgets.QSpinBox()
te = QtWidgets.QTextEdit()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(label)
lay.addWidget(spinBox)
lay.addWidget(te)
listener = ClickListener(self)
listener.clicked.connect(self.onClicked)
listener.addWidget(button)
listener.addWidget(label)
listener.addWidget(spinBox.lineEdit(), spinBox)
listener.addWidget(te.viewport(), te)
def onClicked(self, obj):
print("Clicked, from", obj)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
I am not sure this will be a proper solution or not but I think, you can use the partial method of functools module. A collable object can be treated as a function for the purposes of this module. Here you can see my example,
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
import functools
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(200,200,200,200)
self.button = QPushButton('Button', self)
self.button.move(50,50)
self.label = QLabel(self)
self.label.setText("Label")
self.label.move(100,100)
self.items = [self.button, self.label]
for i in self.items:
i.mousePressEvent = functools.partial(self.getClickedItem, source_object=i)
self.show()
def getClickedItem(self, event, source_object=None):
print("Clicked, from", source_object)
#item text
#print(source_object.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
How can I get the value returned from mousePressEvent of a widget and apply it to another widget?
Here's the widget with the mousePressEvent:
class Text(QTextEdit):
...
def mousePressEvent(self, event):
if event.button()==Qt.LeftButton:
return "test"
Now I want to use the string returned from the event and apply it to another widget:
class OtherWidget(QWidget):
...
self.label=QLabel()
self.label.setText(???) # <=== How to put the string here?
...
How can I do that? I have tried the following but it does not work.
self.label.setText(Text().mousePressEvent())
The events do not return anything for what you point out is impossible, Qt to send information asynchronously uses the signals, in the next part I show an example:
from PyQt5 import QtCore, QtWidgets
class Text(QtWidgets.QTextEdit):
mousePressSignal = QtCore.pyqtSignal(str)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
text = "test: {}-{}".format(event.pos().x(), event.pos().y())
self.mousePressSignal.emit(text)
class OtherWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(OtherWidget, self).__init__(parent)
self.label = QtWidgets.QLabel()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
#QtCore.pyqtSlot(str)
def setText(self, text):
self.label.setText(text)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
t = Text()
o = OtherWidget()
o.resize(640, 480)
t.mousePressSignal.connect(o.setText)
t.show()
o.show()
sys.exit(app.exec_())
I have a QTextBrowser and I want to select a part of the text inside, I need the position of the start and the end of the selection. I want to do that with mousePressEvent and mouseReleaseEvent. Here is my code,
class MainWindow(QMainWindow, TeamInsight.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
def set_text(self):
self.textBrowser.setText('test strings are here')
textBrowser is inside a MainWindow. How do I implement mousePressEvent and mouseReleaseEvent for text in textBrowser
If you want to track events and you can not overwrite the class, the solution is to install an event filter, in your case, just the MouseButtonRelease event, we must filter the viewport() of the QTextBrowser:
import sys
from PyQt5.QtCore import QEvent
from PyQt5.QtWidgets import QMainWindow, QApplication
import TeamInsight
class MainWindow(QMainWindow, TeamInsight.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.browserInput.viewport().installEventFilter(self)
self.browserInput.setText("some text")
def eventFilter(self, obj, event):
if obj is self.browserInput.viewport():
if event.type() == QEvent.MouseButtonRelease:
if self.browserInput.textCursor().hasSelection():
start = self.browserInput.textCursor().selectionStart()
end = self.browserInput.textCursor().selectionEnd()
print(start, end)
elif event.type() == QEvent.MouseButtonPress:
print("event mousePressEvent")
return QMainWindow.eventFilter(self, obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I am using a QDateEdit widget with QDateEdit.setCalendarPopup(True). I am trying to connect a slot to the event when the calendar popup closes. See my example below for my attempts so far, found in MyCalendarWidget. None of my attempts so far have worked. What can I do to perform an action every time the calendar widget popup closes, not only when the date is changed?
from PyQt4 import QtGui, QtCore
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, *args):
super(MainWindow,self).__init__(*args)
self._date = QtGui.QDateEdit()
self._date.setCalendarPopup(True)
self._date.setCalendarWidget(MyCalendarWidget())
self.setCentralWidget(self._date)
class App(QtGui.QApplication):
def __init__(self, *args):
super(App,self).__init__(*args)
self.main = MainWindow()
self.connect(self, QtCore.SIGNAL("lastWindowClosed()"), self.byebye )
self.main.show()
def byebye( self ):
self.exit(0)
class MyCalendarWidget(QtGui.QCalendarWidget):
def __init__(self, parent=None):
print("mycal initialized")
super(MyCalendarWidget, self).__init__(parent)
self.installEventFilter(self)
self._many = 2
self._many2 = 2
def focusInEvent(self, event):
print('-'*self._many + 'focus in')
if self._many == 2:
self._many = 4
else:
self._many = 2
super(MyCalendarWidget, self).focusInEvent(event)
def focusOutEvent(self, event):
print('-'*self._many2+'focus out')
if self._many2 == 2:
self._many2 = 4
else:
self._many2 = 2
super(MyCalendarWidget, self).focusOutEvent(event)
def closeEvent(self, event):
print('close')
super(MyCalendarWidget, self).closeEvent(event)
def mouseReleaseEvent(self, event):
print('mouse')
super(MyCalendarWidget, self).mouseReleaseEvent(event)
def main(args):
global app
app = App(args)
app.exec_()
if __name__ == "__main__":
main(sys.argv)
Figured it out - turns out I need to use the clicked signal in QCalendarWidget. This removes the need to sub-class QCalendarWidget as well.
from PyQt4 import QtGui, QtCore
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, *args):
super(MainWindow,self).__init__(*args)
self._date = QtGui.QDateEdit()
self._date.setCalendarPopup(True)
calendar = self._date.calendarWidget()
calendar.clicked.connect(self._clicked)
self.setCentralWidget(self._date)
def _clicked(self, date):
print('clicked')
class App(QtGui.QApplication):
def __init__(self, *args):
super(App,self).__init__(*args)
self.main = MainWindow()
self.connect(self, QtCore.SIGNAL("lastWindowClosed()"), self.byebye )
self.main.show()
def byebye( self ):
self.exit(0)
def main(args):
global app
app = App(args)
app.exec_()
if __name__ == "__main__":
main(sys.argv)
I'm trying to catch a closeEvent for several dockWidgets that get added dynamically to a QMainWindow. It is unclear to me how I can figure out which widget has been closed.. Here's an simplified example:
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.leftDockWidget = QtGui.QDockWidget('pick tool', self)
self.leftDockWidget.setWidget( QtGui.QLabel('a dock widget') )
self.addDockWidget( QtCore.Qt.LeftDockWidgetArea, self.leftDockWidget )
self.leftDockWidget.closeEvent = self.dockWidgetCloseEvent
self.show()
def dockWidgetCloseEvent(self, event):
print event
# how to get sender widget ?
event.sender() doesn't seem to exist..
any ideas ?
thanks
One way to achieve what you want would be to use an event filter:
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.leftDockWidget = QtGui.QDockWidget('pick tool', self)
self.leftDockWidget.setWidget(QtGui.QLabel('a dock widget'))
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.leftDockWidget)
self.leftDockWidget.installEventFilter(self)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.Close and
isinstance(source, QtGui.QDockWidget)):
print source.windowTitle()
return super(Example, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Example()
window.show()
sys.exit(app.exec_())
def dockWidgetCloseEvent(self, event):
self.sender()