Using PyQt 4.8 and Python 3.3
I'm using a modified version of this example: whereas this example emits a signal on tab press and adds arbitrary text to the second QLineEdit, I want my script to emit a signal on any keypress, add arbitrary signal text to the 2nd QLineEdit, and add the typed character to the 1st QLineEdit (assuming it's a valid ASCII character).
Whenever I try to use any keypress as a signal, I can no longer grab that text to input into QLineEdit. Here's what I have so far and where I'm stuck:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
####################################################################
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
####################################################################
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create objects
self.la = QLabel("Type in this box:")
self.le = MyLineEdit()
self.la2 = QLabel("\nLook here:")
self.le2 = QLineEdit()
self.char = MyLineEdit.char # HOW CAN I GET THIS WORKING?
# layout
layout = QVBoxLayout()
layout.addWidget(self.la)
layout.addWidget(self.le)
layout.addWidget(self.la2)
layout.addWidget(self.le2)
self.setLayout(layout)
# connections
self.connect(self.le, SIGNAL("keyPressed"),
self.update)
def update(self):
newtext1 = self.le.text() + self.char
newtext2 = self.le2.text() + "kP "
self.le.setText(newtext1)
self.le2.setText(newtext2)
####################################################################
class MyLineEdit(QLineEdit):
def __init__(self, *args):
QLineEdit.__init__(self, *args)
def event(self, event):
if (event.type() == QEvent.KeyPress):
self.emit(SIGNAL("keyPressed"))
self.char = "%c" % (event.key())
return True
return QLineEdit.event(self, event)
####################################################################
if __name__ == "__main__":
main()
Any and all help is greatly appreciated. Is there something within PyQt4 that allows me to use a keypress as both a signal and input text, or is my Python off?
Problem1: you are emitting the signal before setting self.char:
class MyLineEdit(QLineEdit):
def __init__(self, *args):
QLineEdit.__init__(self, *args)
self.char = ""
def event(self, event):
if (event.type() == QEvent.KeyPress):
self.char = "%c" % (event.key()) #this line above the next
self.emit(SIGNAL("keyPressed"))
return True
return QLineEdit.event(self, event)
Problem 2: use the char value in your MyLineEdit object:
def update(self):
newtext1 = self.le.text() + self.le.char
newtext2 = self.le2.text() + "kP "
self.le.setText(newtext1)
self.le2.setText(newtext2)
Finally you don't need self.char on MyWindow
Related
I need to create a custom signal on Qmdisubwindow close. In other word, when I closed any subwindow, a signal is emitted with the name of that window being closed. Below is my trail, but seems not right. Error occurs as:
a subwindow already created without calling
add subwindow option is not working
closable action is not working
Hope you can show me how to fix it.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MyMdi(QMdiSubWindow):
sigClosed = pyqtSignal(str)
def __init__(self, parent=None):
super(MyMdi, self).__init__(parent)
def closeEvent(self, event):
"""Get the name of active window about to close
"""
name = name
self.sigClosed.emit('{} is close'.format(name))
QMdiSubWindow.closeEvent(self, event)
class MainWindow(QMainWindow):
count = 0
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.mdi = MyMdi()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.triggered[QAction].connect(self.windowaction)
self.setWindowTitle("MDI demo")
# my signal
self.mdi.sigClosed.connect(self.windowclosed)
#pyqtSlot(str)
def windowclosed(self, text):
print(text)
def windowaction(self, q):
if q.text() == "New":
MainWindow.count = MainWindow.count+1
sub = QMdiSubWindow()
sub.setWidget(QTextEdit())
sub.setWindowTitle("subwindow"+str(MainWindow.count))
self.mdi.addSubWindow(sub)
sub.show()
def main():
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You have an initial error: a QMdiSubWindow must be inside a QMdiArea but there is none in your code.
On the other hand, the idea of subclassing is good but you have several drawbacks:
You are not using it initially since there is no QMdiArea, if you execute the QAction then your application will be closed because a QMdiSubWindow does not have any method called addSubWindow.
The QMdiSubWindow does not have an attribute called name, you must use windowTitle.
class MdiSubWindow(QMdiSubWindow):
sigClosed = pyqtSignal(str)
def closeEvent(self, event):
"""Get the name of active window about to close
"""
self.sigClosed.emit(self.windowTitle())
QMdiSubWindow.closeEvent(self, event)
class MainWindow(QMainWindow):
count = 0
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.triggered[QAction].connect(self.windowaction)
self.setWindowTitle("MDI demo")
#pyqtSlot(str)
def windowclosed(self, text):
print(text)
def windowaction(self, q):
if q.text() == "New":
MainWindow.count = MainWindow.count + 1
sub = MdiSubWindow()
sub.setWidget(QTextEdit())
sub.setAttribute(Qt.WA_DeleteOnClose)
sub.setWindowTitle("subwindow" + str(MainWindow.count))
sub.sigClosed.connect(self.windowclosed)
self.mdi.addSubWindow(sub)
sub.show()
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 made a ui for work using PyQt5 and Python3. Additionally to clicking the buttons, I want to execute specific actions by pressing specific keys on my keyboard e.g. the space bar. I used the following to do so:
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Space:
print('space')
elif key == Qt.Key_W:
print('w')
Instead of printing 'space', it presses the focused button in the ui, when I'm pressing the space bar. Just like I hit return. Although pressing the 'W'-key prints 'w' as expected.
I already searched here at stackoverflow and other where in the web as well, but I found nothing really helpfull. The best I got was this one here. But it's using PyQt4.3 and copy and paste the code to my editor just brought me some errors. I thought the approach was a good one, but I were not able to transfer this to PyQt5.
Try it:
#from PyQt4.QtCore import *
#from PyQt4.QtGui import *
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create objects
self.la = QLabel("Press Space or `w` in this box:")
self.le = MyLineEdit()
self.la2 = QLabel("\nLook here:")
self.le2 = QLineEdit()
self.btnSpace = QPushButton("Button1")
self.btnSpace.setEnabled(False)
self.btnSpace.clicked.connect(self.onBtnSpace)
self.btnW = QPushButton("Button2")
self.btnW.setEnabled(False)
self.btnW.clicked.connect(self.onBtnW)
# layout
layout = QVBoxLayout()
layout.addWidget(self.la)
layout.addWidget(self.le)
layout.addWidget(self.la2)
layout.addWidget(self.le2)
layout.addWidget(self.btnSpace)
layout.addWidget(self.btnW)
self.setLayout(layout)
# connections
#self.connect(self.le, SIGNAL("tabPressed"), self.update)
self.le.signalTabPressed[str].connect(self.update)
def update(self, keyPressed):
newtext = str(self.le2.text()) + str(keyPressed) #"tab pressed "
self.le2.setText(newtext)
if keyPressed == "Key Space; ":
self.btnSpace.setEnabled(True)
self.btnSpace.setText(str(keyPressed))
if keyPressed == "Key W; ":
self.btnW.setEnabled(True)
self.btnW.setText(str(keyPressed))
def onBtnSpace(self):
print("keyPressed -> btnSpace")
def onBtnW(self):
print("keyPressed -> btnW")
class MyLineEdit(QLineEdit):
signalTabPressed = pyqtSignal(str) # +++
def __init__(self, *args):
QLineEdit.__init__(self, *args)
def event(self, event):
#if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_Tab):
if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_Space):
self.signalTabPressed.emit("Key Space; ")
return True
if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_W):
self.signalTabPressed.emit("Key W; ")
return True
return QLineEdit.event(self, event)
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
With S. Nick's very useful answer I were able to come to a solution that really fits my needs.
class MyPushButton(QPushButton):
def __init__(self, *args):
QPushButton.__init__(self, *args)
def event(self, event):
if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Space):
print(foo)
return True
return QPushButton.event(self, event)
So, that's it. Whenever I press the space key on my keyboard now it just prints foo and nothing else. Where print(foo) is just a place-holder for whatever I wish to do, obviously.
I'm new to python and pyqt.
I'm learning how to use threading with GUI.
I followed this tutorial
http://www.xyzlang.com/python/PyQT5/pyqt_multithreading.html
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import threading
from _ast import While
class Communicate(QObject):
signal = pyqtSignal(int, str)
class My_Gui(QWidget):
def __init__(self):
super().__init__()
self.comm = Communicate()
self.comm.signal.connect(self.append_data)
self.initUI()
def initUI(self):
btn_count = QPushButton('Count')
btn_count.clicked.connect(self.start_counting)
self.te = QTextEdit()
vbox = QVBoxLayout()
vbox.addWidget(btn_count)
vbox.addWidget(self.te)
self.setLayout(vbox)
self.setWindowTitle('MultiThreading in PyQT5')
self.setGeometry(400, 400, 400, 400)
self.show()
def count(self, comm):
'''
for i in range(10):
data = "Data "+str(i)
comm.signal.emit(i, data)
'''
i = 0
while True:
data = "Data "+str(i)
comm.signal.emit(i, data)
i+=1
def start_counting(self):
my_Thread = threading.Thread(target=self.count, args=(self.comm,))
my_Thread.start()
def append_data(self, num, data):
self.te.append(str(num) + " " + data)
if __name__ == '__main__':
app = QApplication(sys.argv)
my_gui = My_Gui()
sys.exit(app.exec_())
I changed the for loop to infinite while loop(incrementing the 'i').
If I execute the program, the GUI still hangs but if I remove the emit signal inside the loop, it no longer hangs.
Are there some tricks to make it not hangs?
while True makes an endless loop in the background
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import threading
from _ast import While
class Communicate(QObject):
signal = pyqtSignal(int, str)
class My_Gui(QWidget):
def __init__(self):
super().__init__()
self.comm = Communicate()
self.comm.signal.connect(self.append_data)
self.initUI()
def initUI(self):
btn_count = QPushButton('Count')
btn_count.clicked.connect(self.start_counting)
self.te = QTextEdit()
vbox = QVBoxLayout()
vbox.addWidget(btn_count)
vbox.addWidget(self.te)
self.setLayout(vbox)
self.setWindowTitle('MultiThreading in PyQT5')
self.setGeometry(400, 400, 400, 400)
self.show()
def count(self, comm):
for i in range(10):
data = "Data "+str(i)
comm.signal.emit(i, data)
# While True below will never stop and cause your program to stuck
'''
i = 0
while True:
data = "Data "+str(i)
comm.signal.emit(i, data)
i+=1
'''
def start_counting(self):
my_Thread = threading.Thread(target=self.count, args=(self.comm,))
my_Thread.start()
def append_data(self, num, data):
self.te.append(str(num) + " " + data)
if __name__ == '__main__':
app = QApplication(sys.argv)
my_gui = My_Gui()
sys.exit(app.exec_())
I think you are getting downvoted for two things:
you did only copy and paste the code from the tutorial
you didn't read the tutorial
In the tutorial, the author states:
In the above example, we have created a QPushbutton and QTextEdit. When the button is clicked it creates a new Thread that counts from 0 to 9 and emits the signal in order to append the number and data in the QTextEdit. In class Communicate signal is initialized as pyqtSignal(int, str). This int and str means when a signal will be emitted it will also pass two arguments the first one will be of Integer type and second one will be of String type.
By changing the loop to while true you continuosly emit signals and append the text in the QTextEdit. Probably not what you want.
Also commenting the emit statement internally continues to run the while loop.
I want to react on a mouseclick on a QLabel.
To achieve this I have redefined the method mouseReleaseEvent of QLabel.
I want to pass two arguments to the slot:
- the QtGui.QMouseEvent
- an ID of the clicked QLabel
But I can only pass one parameter. Either QtGui.QMouseEvent or the ID.
The combination does not work.
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
class ExtendedQLabel(QtGui.QLabel):
#labelClickSignal_1 = pyqtSignal(QtGui.QMouseEvent)
labelClickSignal_1 = pyqtSignal(QtGui.QMouseEvent, int)
labelClickSignal_2 = pyqtSignal()
def __init(self, parent):
QtGui.QLabel.__init__(self, parent)
# redefinition
def mouseReleaseEvent(self, event):
#self.labelClickSignal_1.emit(event)
self.labelClickSignal_1.emit(event, 0)
self.labelClickSignal_2.emit()
class Test(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.names = ['Test1', 'Test2', 'Test3']
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.grid = QtGui.QGridLayout(self.centralWidget)
row = 0
for name in self.names:
self.addLabel(name, row)
row = row + 1
def addLabel(self, name, row):
label = ExtendedQLabel(name)
# QtGui.QMouseEvent is automatically passed to the slot
label.labelClickSignal_1.connect(self.onLabelClicked_1)
# The row ID is passed to the slot
label.labelClickSignal_2.connect(lambda id = row:
self.onLabelClicked_2(id))
# *This does not work*
label.labelClickSignal_1.connect(lambda id = row:
self.onLabelClicked_3(QtGui.QMouseEvent, id))
self.grid.addWidget(label, row, 1)
row = row + 1
def onLabelClicked_1(self, event):
if event.button() == QtCore.Qt.RightButton:
print('right')
else:
print('left')
def onLabelClicked_2(self, id):
print('Label {0} clicked'.format(id))
def onLabelClicked_3(self, event, id):
# *This does not work*
if event.button() == QtCore.Qt.RightButton:
print('right {0}'.format(id))
else:
print('left {0}'.format(id))
def main():
app = QtGui.QApplication(sys.argv)
t = Test()
t.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Ok, since your code had several pieces that did not work I rewrote the important Parts to achieve what you want. Please Note that I use PySide and not PyQt. This means you have to change the importe Statements and the Signal back to PyQt Notation.
The rest is explained in the code.
import sys
from PySide import QtGui, QtCore
class ExtendedQLabel(QtGui.QLabel):
#Signal that emits on MouseRelease
labelClickSignal_1 = QtCore.Signal(QtGui.QMouseEvent, int)
# init to -1
labelId = -1
# This is the new Constructor, Please note the double underscore
# before and behind `init`
def __init__(self, parent, labelId):
self.labelId = labelId
QtGui.QLabel.__init__(self, parent)
# emit labelClickSignal
def mouseReleaseEvent(self, event):
self.labelClickSignal_1.emit(event, self.labelId)
class Test(QtGui.QMainWindow):
def __init__(self, parent=None):
# same as yours [...]
def addLabel(self, name, row):
# please note the added argument
label = ExtendedQLabel(name,row)
# connect the signal
label.labelClickSignal_1.connect(self.onLabelClicked_1)
self.grid.addWidget(label, row, 1)
row = row + 1
def onLabelClicked_1(self, event,labelId):
if event.button() == QtCore.Qt.RightButton:
print('right')
print(labelId)
else:
print('left')
print(labelId)
OLD ANSWER
You have to define your Signal to support your two arguments:
labelClickSignal_1 = pyqtSignal(QtGui.QMouseEvent,int)
See here for additional information.
Example from the docs:
from PyQt4.QtCore import QObject, pyqtSignal
class Foo(QObject):
# This defines a signal called 'closed' that takes no arguments.
closed = pyqtSignal()
# This defines a signal called 'rangeChanged' that takes two
# integer arguments.
range_changed = pyqtSignal(int, int, name='rangeChanged')
# This defines a signal called 'valueChanged' that has two overloads,
# one that takes an integer argument and one that takes a QString
# argument. Note that because we use a string to specify the type of
# the QString argument then this code will run under Python v2 and v3.
valueChanged = pyqtSignal([int], ['QString'])
I think I solved it. Your lambda with the comment *and this does not work* took only one argument, though the slot is defined to take two. So I just skipped the first argument.
Next issue: It is difficult to pass Events that have been generated by system. I think they are destroyed after they have been handled. So I copied the data of the event into a namedtuple and passed this instead. (I also tried to copy the event, but this did not work somehow.)
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
from collections import namedtuple
class ExtendedQLabel(QtGui.QLabel):
MouseEventTuple = namedtuple('MouseEventTuple', 'type pos button buttons modifiers')
#labelClickSignal_1 = pyqtSignal(QtGui.QMouseEvent)
labelClickSignal_1 = pyqtSignal(MouseEventTuple, int)
labelClickSignal_2 = pyqtSignal()
def __init(self, parent):
QtGui.QLabel.__init__(self, parent)
# redefinition
def mouseReleaseEvent(self, event):
eventTuple = ExtendedQLabel.MouseEventTuple(type = event.type(), pos = event.pos(), button = QtCore.Qt.RightButton, buttons = event.buttons(), modifiers = event.modifiers())
self.labelClickSignal_1.emit(eventTuple, 0)
self.labelClickSignal_2.emit()
class Test(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.names = ['Test1', 'Test2', 'Test3']
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.grid = QtGui.QGridLayout(self.centralWidget)
row = 0
for name in self.names:
self.addLabel(name, row)
row = row + 1
def addLabel(self, name, row):
label = ExtendedQLabel(name)
# QtGui.QMouseEvent is automatically passed to the slot
label.labelClickSignal_1.connect(self.onLabelClicked_1)
# The row ID is passed to the slot
label.labelClickSignal_2.connect(lambda id = row:
self.onLabelClicked_2(id))
# *This works now*
label.labelClickSignal_1.connect(lambda _unused_, id = row:
self.onLabelClicked_3(QtGui.QMouseEvent, id))
self.grid.addWidget(label, row, 1)
row = row + 1
def onLabelClicked_1(self, eventTuple):
if eventTuple.button == QtCore.Qt.RightButton:
print('right')
else:
print('left')
def onLabelClicked_2(self, id):
print('Label {0} clicked'.format(id))
def onLabelClicked_3(self, eventTuple, id):
# *This does not work*
if eventTuple.button == QtCore.Qt.RightButton:
print('right {0}'.format(id))
else:
print('left {0}'.format(id))
def main():
app = QtGui.QApplication(sys.argv)
t = Test()
t.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()