Trigger Close Event for Tabs in PySide QTabWidget - python

I've written 3 separate widgets and each one contains a'closeEvent'. However the close events are NOT triggered when the widgets are placed as tabs inside the MainWindow. How can I emit a signal to properly fire the 'closeEvent' for each tab when the MainWindow closes?
I've done my best to simplify the code to focus on the main goal. I'd like to emit the close event for each tab so in the future when I add more tabs it will be more flexible and each tool will still be self contained.
import sys
from PySide import QtGui, QtCore
class TabA(QtGui.QWidget):
def __init__(self, parent=None):
super(TabA, self).__init__(parent)
self.resize(300, 300)
self.setWindowTitle('App A')
self.btn = QtGui.QPushButton("Button A")
grid = QtGui.QGridLayout()
grid.addWidget(self.btn)
self.setLayout(grid)
def closeEvent(self, event):
print "closing Tab A"
event.accept()
class TabB(QtGui.QWidget):
def __init__(self, parent=None):
super(TabB, self).__init__(parent)
self.resize(300, 300)
self.setWindowTitle('App A')
self.btn = QtGui.QPushButton("Button A")
grid = QtGui.QGridLayout()
grid.addWidget(self.btn)
self.setLayout(grid)
def closeEvent(self, event):
print "closing Tab A"
event.accept()
class TabC(QtGui.QWidget):
def __init__(self, parent=None):
super(TabC, self).__init__(parent)
self.resize(300, 300)
self.setWindowTitle('App A')
self.btn = QtGui.QPushButton("Button A")
grid = QtGui.QGridLayout()
grid.addWidget(self.btn)
self.setLayout(grid)
def closeEvent(self, event):
print "closing Tab A"
event.accept()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.initUI()
def initUI(self):
self.resize(300, 300)
self.setWindowTitle('Tabs')
# Tabs
main_tabWidget = QtGui.QTabWidget()
main_tabWidget.addTab(TabA(), "TabA")
main_tabWidget.addTab(TabB(), "TabB")
main_tabWidget.addTab(TabC(), "TabC")
vbox = QtGui.QVBoxLayout()
vbox.addWidget(main_tabWidget)
vbox.setContentsMargins(0, 0, 0, 0)
main_widget = QtGui.QWidget()
main_widget.setLayout(vbox)
self.setCentralWidget(main_widget)
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

The closeEvent method is only called for a top-level window when a close request is received, so it is not suitable for your use-case. However, there are several signals that are emitted while the application is shutting down that you can connect to.
So for instance you could do this:
class TabA(QtGui.QWidget):
def __init__(self, parent=None):
super(TabA, self).__init__(parent)
...
QtGui.qApp.aboutToQuit.connect(self.handleClose)
def handleClose(self):
print "closing Tab A"
This signal is emitted just before the event-loop exits, and there's also a QtGui.qApp.lastWindowClosed signal which is emitted just after the main window closes.
(PS: don't be tempted to use __del__ for this purpose, because the exact behaviour during shutdown is strictly undefined in PyQt, and can change between versions).

Related

QStackedWidget opening multiple windows

I have build a small application using PyQt and as far as I know QStackedWidget is used for making multipage applications. The issue is that it is opening multiple pages and also I'm unable to redirect to class Xyz using button in class Abc.
main.py
from PyQt4 import QtGui, QtCore
import sys
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.stacked_widget = QtGui.QStackedWidget()
layout = QtGui.QVBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.addWidget(self.stacked_widget)
widget = QtGui.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
widget_1 = Abc(self.stacked_widget)
widget_2 = Xyz(self.stacked_widget)
self.stacked_widget.addWidget(widget_1)
self.stacked_widget.addWidget(widget_2)
self.showMaximized()
class Abc(QtGui.QWidget):
def __init__(self, stacked_widget, parent=None):
super(Abc, self).__init__(parent)
self.stacked_widget = stacked_widget
self.frame = QtGui.QFrame()
self.frame_layout = QtGui.QVBoxLayout()
self.button = QtGui.QPushButton('Click me!')
self.button.clicked.connect(self.click)
self.frame_layout.addWidget(self.button)
self.frame.setLayout(self.frame_layout)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.frame)
self.setLayout(self.layout)
self.showMaximized()
def click(self):
print("You clicked me!")
self.stacked_widget.setCurrentIndex(1)
class Xyz(QtGui.QWidget):
def __init__(self, parent=None):
super(Xyz, self).__init__(parent)
self.frame_layout = QtGui.QStackedLayout()
self.page1 = Page("Page 1", self.frame_layout)
self.page2 = Page("Page 2", self.frame_layout)
self.frame_layout.addWidget(self.page1)
self.frame_layout.addWidget(self.page2)
class Page(QtGui.QWidget):
def __init__(self, text, frame_layout, parent=None):
super(Page, self).__init__(parent)
print(self.width(), self.height())
self.frame_layout = frame_layout
self.frame = QtGui.QFrame()
self.frame_layout = QtGui.QVBoxLayout()
self.frame.setStyleSheet("background-color: rgb(191, 191, 191)")
self.label = QtGui.QLabel()
self.label.setText(text)
self.frame_layout.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
self.frame.setLayout(self.frame_layout)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.frame)
self.layout.setContentsMargins(0,0,0,0)
self.setLayout(self.layout)
self.showMaximized()
print(self.width(), self.height())
if __name__ == "__main__":
app = QtGui.QApplication([])
window = Window()
window.show()
sys.exit(app.exec_())
The main problem is that you are not setting the QStackedLayout for the Xyz widget, the result is that all pages will actually appear as top level windows.
When a widget is added to a layout, it takes ownership of it; if the layout is already set to a widget, then the widget that was added to the layout becomes reparented to the other. The same happens if you set the layout afterwards.
Why does it show the first page as a separate window? And why doesn't it show the second?
When a new widget is created without a parent, it becomes a top level window; when a widget is added to a new stacked layout, Qt automatically tries to show it; you didn't set the layout to anything (which would reparent it as explained before), and the result is that the first page is shown as a standalone window.
Now, since the first "screen" is going to be shown only the first time, you can set that widget as the central widget, and then set a Xyz instance (which is actually a QStackedWidget subclass) as a new central widget.
Note that you don't need to use QWidget to add a QFrame if that's the only parent widget shown: you can just subclass QFrame.
This is a much simpler and cleaner version of your code:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.startPage = Abc()
self.setCentralWidget(self.startPage)
self.startPage.startRequest.connect(self.buildPages)
def buildPages(self):
self.pages = Xyz()
self.setCentralWidget(self.pages)
class Abc(QtGui.QFrame):
startRequest = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Abc, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton('Click me!')
self.button.clicked.connect(self.startRequest)
layout.addWidget(self.button)
class Xyz(QtGui.QStackedWidget):
def __init__(self, parent=None):
super(Xyz, self).__init__(parent)
self.page1 = Page("Page 1")
self.page2 = Page("Page 2")
self.addWidget(self.page1)
self.addWidget(self.page2)
self.page1.switchRequest.connect(lambda: self.setCurrentIndex(1))
self.page2.switchRequest.connect(lambda: self.setCurrentIndex(0))
class Page(QtGui.QFrame):
switchRequest = QtCore.pyqtSignal()
def __init__(self, text, parent=None):
super(Page, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
# set the background only for QFrame subclasses (which also includes
# QLabel), this prevents setting the background for other classes,
# such as the push button
self.setStyleSheet('''
QFrame {
background-color: rgb(191, 191, 191)
}
''')
# when adding a widget to a layout, the layout tries to automatically
# make it as big as possible (based on the widget's sizeHint); so you
# should not use the alignment argument for layout.addWidget(), but for
# the label instead
self.label = QtGui.QLabel(text, alignment=QtCore.Qt.AlignCenter)
layout.addWidget(self.label)
self.switchButton = QtGui.QPushButton('Switch')
layout.addWidget(self.switchButton)
self.switchButton.clicked.connect(self.switchRequest)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

What is the best way of deleting a qpushbutton

Hello I need to know a way of deleting a QPushButton from my interface, i have not found a way that i
s usefull for my program.
for example:
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__init__(title, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
self.setText(e.mimeData().text())
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
edit = QtGui.QLineEdit('', self)
edit.setDragEnabled(True)
edit.move(30, 65)
button = Button("Button", self)
button.move(190, 65)
self.setWindowTitle('Simple drag & drop')
self.setGeometry(300, 300, 300, 150)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
if __name__ == '__main__':
main()
there i created a button that can be dragged and dropped in my main window
now i want a way to delete this button from the mainwindow while im in the program.
thanks

Error with thread in pyqt4

I have the problem. I made the thread and from there I want to open the new window. But it does not work.
import sys
from grab import Grab
from PyQt4 import QtGui, QtCore
class Requests(QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def run(self):
# here some comands
self.emit(QtCore.SIGNAL("mysignal(QString)"), 'open')
class window(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(100, 100, 500, 200)
self.setWindowTitle('Window')
self.label = QtGui.QLabel(u'Hello')
self.Layout = QtGui.QVBoxLayout()
self.Layout.addWidget(self.label)
self.setLayout(self.Layout)
self.c = Requests()
self.c.start()
self.connect(self.c, QtCore.SIGNAL("mysignal(QString)"), self.open_window, QtCore.Qt.QueuedConnection)
def open_window(self):
print 'open modal window'
modal_w = popup_window()
modal_w.show()
app = QtGui.QApplication(sys.argv)
main = window()
main.show()
sys.exit(app.exec_())
It is not show new window. Where is the error?
You need to connect the signal before the thread starts, and hence before the signal is emitted. If you want to show a dialog when the worker thread completes, just use the finished signal:
class Requests(QtCore.QThread):
def run(self):
# do some work...
print 'work finished'
...
self.c = Requests()
self.c.finished.connect(self.open_window)
self.c.start()
You also need to keep a reference to the dialog when opening it in the slot:
def open_window(self):
print 'open modal window'
self.modal_w = popup_window()
self.modal_w.show()

PyQT Button click doesn't work

So my problem is that instead of manually writing a ton of code for a bunch of buttons, I want to create a class for a QPushButton and then change so many variables upon calling that class to create my individual buttons.
My problem is that my button does not seem to be clickable despite calling the clicked.connect function and having no errors upon running the code. Here are the relevant parts of the button class:
class Button(QtGui.QPushButton):
def __init__(self, parent):
super(Button, self).__init__(parent)
self.setAcceptDrops(True)
self.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.setText("Change Me!")
def retranslateUi(self, Form):
self.clicked.connect(self.printSomething)
def printSomething(self):
print "Hello"
Here is how I call the button class:
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.btn = Button(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setLayout(layout)
You should perform the connection to the clicked signal on the __init__ method:
from PyQt4 import QtGui,QtCore
class Button(QtGui.QPushButton):
def __init__(self, parent):
super(Button, self).__init__(parent)
self.setAcceptDrops(True)
self.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.setText("Change Me!")
self.clicked.connect(self.printSomething) #connect here!
#no need for retranslateUi in your code example
def printSomething(self):
print "Hello"
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.btn = Button(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setLayout(layout)
app = QtGui.QApplication([])
w = MyWindow()
w.show()
app.exec_()
You can run it and will see the Hello printed on the console every time you click the button.
The retranslateUi method is for i18n. You can check here.

PyQt how to get sender (widget) in closeEvent?

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()

Categories

Resources