I have a class BooleanButton contains extra boolean flag to toggle every click. After each click, I want it to emit a signal and the slot will receive the boolean flag. I just write the following, but of course, it won't work.
class BooleanButton(QPushButton):
def __init__(self, name):
QPushButton.__init__(self, name)
self.bool = False
def clicked(self, bool):
self.bool = not self.bool
self.emit(self.bool)
After creating the object, it connects to a slot. When I click this button, a swapping true-false signal will send to the slot.
bool_btn.isclicked[bool].connect(widget.func)
Thanks.
First, don't call a method clicked, that will hide the buttons clicked signal.
If you want to define a new signal, you need to do so using QtCore.pyqtSignal, then you can connect the clicked singal to a slot that will in turn emit your custom signal. Example:
class BooleanButton(QPushButton):
isclicked = pyqtSignal(bool)
def __init__(self, name):
QPushButton.__init__(self, name)
self.bool = False
self.clicked.connect(self.on_clicked)
def on_clicked(self, bool):
self.bool = not self.bool
self.isclicked.emit(self.bool)
As three_pineapples said, QPushButton comes with this feature built-in. Here's a simple example illustrating this behaviour.
from PyQt4 import QtGui, QtCore
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.button = QtGui.QPushButton("Click me", self)
self.button.setCheckable(True)
self.lineEdit = QtGui.QLineEdit(self)
self.button.clicked.connect(self.onClicked)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
layout.addWidget(self.lineEdit)
def onClicked(self, checked):
if checked:
self.lineEdit.setText("Button checked")
else:
self.lineEdit.setText("Button unchecked")
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
So your BooleanButton is actually just a QPushButton.
Related
Don't know exactly how to give the parameters of QMouseEvent class. Should I create new class to implement a QMouseEvent into my QTextEdit?
class Test(QMainWindow):
def __init__(self):
super().__init__()
self.txt = QTextEdit(self)
self.txt.setMouseTracking(True)
self.txt.mouseReleaseEvent(QMouseEvent())
class Test2(QTextEdit):
def __init__(self):
super().__init__()
def mouseReleaseEvent(self, e):
print("text edit is clicked")
ui = Test()
ui.show()
Since many times it is asked how to detect the events that affect a widget then in this answer I will detail the solution and it will be used as a canonical answer for future questions.
To detect an event from a widget there are several solutions:
- Override a method
If the widget has a method that handles that event then an option is to override that method and associate it with a signal so that other objects can be notified.
In the particular case of the mouse release event, this is handled by the mouseReleaseEvent method.
from PyQt5 import QtCore, QtWidgets
class TextEdit(QtWidgets.QTextEdit):
released = QtCore.pyqtSignal()
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
self.released.emit()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.textedit = TextEdit()
self.textedit.released.connect(self.handle_released)
self.setCentralWidget(self.textedit)
#QtCore.pyqtSlot()
def handle_released(self):
print("released")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
- Use an event filter
Qt allows you to monitor the events using an event filter, so you can take advantage of this feature to emit a signal in a similar way to the previous solution.
In the case of classes that inherit from QAbstractScrollArea, the mouse methods are transmitted to the viewport, so that object must be monitored.
from PyQt5 import QtCore, QtWidgets
class ReleaseFilter(QtCore.QObject):
released = QtCore.pyqtSignal()
def __init__(self, widget):
super().__init__(widget)
self._widget = widget
self.widget.installEventFilter(self)
#property
def widget(self):
return self._widget
def eventFilter(self, obj, event):
if obj is self.widget and event.type() == QtCore.QEvent.MouseButtonRelease:
self.released.emit()
return super().eventFilter(obj, event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.textedit = QtWidgets.QTextEdit()
rf = ReleaseFilter(self.textedit.viewport())
rf.released.connect(self.handle_released)
self.setCentralWidget(self.textedit)
#QtCore.pyqtSlot()
def handle_released(self):
print("released")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
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'm having though time figuring out what kind of signal is emitted in following situation:
Basicly that's QScrollArea that holds multiple QTableWidgets:
class ScrollArea(QtGui.QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
self.scroll_widget = QtGui.QWidget()
self.scroll_layout = QtGui.QVBoxLayout()
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setWidgetResizable(True)
self.__create_content()
self.setWidget(self._content_widget)
self.scroll_layout.addWidget(self)
self.scroll_widget.setLayout(self.scroll_layout)
def __create_content(self):
self._content_widget = QtGui.QWidget()
self._content_widget_layout = QtGui.QVBoxLayout()
self._content_widget.setLayout(self._content_widget_layout)
def add_item(self, item):
self._content_widget_layout.addWidget(item)
I'm using Plastique style for QApplication. As it can be seen from the above picture, when an item is clicked inside QScrollArea, blue border appears. What I would like to know is which signal is emitted when the border is drawn? I need this information so I can append a row to the selected QTableWidget whenever a button (on the left side) is clicked.
Also you can see that there is a 'x' inside each table, when 'x' is pressed that QTableWidget gets removed from QScrollArea. If there is a solution for previous problem, I could also remove QTableWidget depending on user selection rather than user clicking the 'x'.
To get the widget that has the focus you can use the focusChanged signal of QApplication:
from PyQt4 import QtCore, QtGui
class HorizontalHeader(QtGui.QHeaderView):
def __init__(self, parent=None):
super(HorizontalHeader, self).__init__(QtCore.Qt.Horizontal, parent)
self.button = QtGui.QToolButton(self, text="x")
self.sectionResized.connect(self.handleSectionResized)
def handleSectionResized(self):
last_ix = self.count() - 1
pos = QtCore.QPoint(self.sectionViewportPosition(last_ix) + self.sectionSize(last_ix) , 0)
self.button.move(pos)
def showEvent(self, event):
self.handleSectionResized()
super(HorizontalHeader, self).showEvent(event)
class TableView(QtGui.QTableView):
def __init__(self, *args, **kwargs):
super(TableView, self).__init__(*args, **kwargs)
header = HorizontalHeader(self)
header.button.clicked.connect(self.deleteLater)
self.setHorizontalHeader(header)
QtGui.qApp.focusChanged.connect(self.onFocusChanged)
def onFocusChanged(self, old, new):
if new == self:
self.deleteLater()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
scrollArea = QtGui.QScrollArea()
scrollArea.setWidgetResizable(True)
widget = QtGui.QWidget()
scrollArea.setWidget(widget)
lay = QtGui.QVBoxLayout(widget)
for i in range(10):
w = TableView()
model = QtGui.QStandardItemModel(4, 2, w)
w.setModel(model)
lay.addWidget(w)
scrollArea.show()
sys.exit(app.exec_())
I made the simple code below as example. It justs open a new window clicking on a button. I don't find a way to prevent this widget to be re-opened if it is already on the screen. I would like to open a QDialog warning if the window already exists and mainly to have the closeEvent method sending a signal to Mainwidget saying that the new window has been closed. This would allow to open the newWidget again.
import sys
from PyQt4 import QtCore, QtGui
class NewWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(NewWidget,self).__init__(parent)
self.lineEdit = QtGui.QLineEdit('new window',self)
self.resize(200,50)
self.show()
def closeEvent(self,ev):
self.Exit = QtGui.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)
ev.ignore()
if self.Exit == QtGui.QMessageBox.Yes:
ev.accept()
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWidget,self).__init__(parent)
self.button = QtGui.QPushButton("button", self)
self.button.clicked.connect(self.open_new)
def open_new(self):
self.new = NewWidget()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main = MainWidget()
main.resize(200,50)
main.move(app.desktop().screen().rect().center() - main.rect().center())
main.show()
sys.exit(app.exec_())
I think a better solution is to avoid creating a new window every time you click the button.
One way to do this would be to change the subwindow to a QDialog:
class NewWidget(QtGui.QDialog):
...
and move the resize/show lines into the open_new method:
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
...
self._subwindow = None
def open_new(self):
if self._subwindow is None:
self._subwindow = NewWidget(self)
self._subwindow.resize(200, 50)
# move it next to the main window
pos = self.frameGeometry().topLeft()
self._subwindow.move(pos.x() - 250, pos.y())
self._subwindow.show()
self._subwindow.activateWindow()
So now there is only ever one subwindow, which just gets re-activated whenever the button is clicked.
Great. The final solution of my problem looks like this :
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
...
self._subwindow = QtGui.Qdialog()
def open_new(self):
if self.subwindow.isVisible() is False:
self._subwindow = NewWidget(self)
self._subwindow.resize(200, 50)
# move it next to the main window
pos = self.frameGeometry().topLeft()
self._subwindow.move(pos.x() - 250, pos.y())
self._subwindow.show()
self._subwindow.activateWindow()
I did something similar to the answer here.
The GUI is simple, you click a button which starts a thread that instead of emitting signals it sends events. The events cause a label to change text.
Here's the code:
from PySide.QtGui import *
from PySide.QtCore import *
import sys, time
class MyEvent(QEvent):
def __init__(self, message):
super().__init__(QEvent.User)
self.message = message
class MyThread(QThread):
def __init__(self, widget):
super().__init__()
self._widget = widget
def run(self):
for i in range(10):
app.sendEvent(self._widget, MyEvent("Hello, %s!" % i))
time.sleep(.1)
class MyReceiver(QWidget):
def __init__(self, parent=None):
super().__init__()
layout = QHBoxLayout()
self.label = QLabel('Test!')
start_button = QPushButton('Start thread')
start_button.clicked.connect(self.startThread)
layout.addWidget(self.label)
layout.addWidget(start_button)
self.setLayout(layout)
def event(self, event):
if event.type() == QEvent.User:
self.label.setText(event.message)
return True
return False
def startThread(self):
self.thread = MyThread(self)
self.thread.start()
app = QApplication(sys.argv)
main = MyReceiver()
main.show()
sys.exit(app.exec_())
The problem is that, only the first event get processed by MyReceiver, then the widget freezes!.
Any clues?. Thanks
The behaviour of the event method of QWidget is altered in your code: you should
let the base class decide on what to do with events, not returning False if it is
not a custom event. Do it like this:
def event(self, event):
if event.type() == QEvent.User:
self.label.setText(event.message)
return True
return QWidget.event(self, event)
This fixes your problem.
Also, you may prefer to emit a Qt signal from the thread, and have it connected in
your widget to some method to change the label - signals and slots are thread-safe
in Qt 4 (contrary to Qt 3). It will achieve the same result.