I am trying to build an app using PyQt5 that has a secondary window "popup" when an item in a QListWidget
is double clicked.
Here is the code:
import sys
from PyQt5.QtWidgets import QWidget, QListWidget, QListWidgetItem, QLabel, QPushButton, QApplication
class exampleWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
listWidget = QListWidget(self)
listWidget.itemDoubleClicked.connect(self.buildExamplePopup)
names = ["Jack", "Chris", "Joey", "Kim", "Duncan"]
for n in names:
QListWidgetItem(n, listWidget)
self.setGeometry(100, 100, 100, 100)
self.show()
@staticmethod
def buildExamplePopup(item):
name = item.text()
exPopup = examplePopup(name)
exPopup.setGeometry(100, 200, 100, 100)
exPopup.show()
class examplePopup(QWidget):
def __init__(self, name):
super().__init__()
self.name = name
self.initUI()
def initUI(self):
lblName = QLabel(self.name, self)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = exampleWidget()
sys.exit(app.exec_())
I want a second window to popup when one of the names in the listbox is double clicked, but I can't for the life of me get the examplePopup
widget to draw on screen. Thanks in advance for the help.
Dialogs are useful GUI components that allow you to communicate with the user (hence the name dialog). They are commonly used for file Open/Save, settings, preferences, or for functions that do not fit into the main UI of the application.
PyQt5 Tutorial — Getting started with PyQt5 In Qt (and most User Interfaces) 'widget' is the name given to a component of the UI that the user can interact with. User interfaces are made up of multiple widgets, arranged within the window.
PyQt5 is the latest version of a GUI widgets toolkit developed by Riverbank Computing. It is a Python interface for Qt, one of the most powerful, and popular cross-platform GUI library. PyQt5 is a blend of Python programming language and the Qt library.
The popup window doesn't show because you are not keeping a reference to it, and so it gets garbage-collected as soon as buildExamplePopup
returns.
You can easily fix the problem like this:
def buildExamplePopup(self, item):
name = item.text()
self.exPopup = examplePopup(name)
self.exPopup.setGeometry(100, 200, 100, 100)
self.exPopup.show()
I can't really tell why it's not working with QWidget
since it does work if the second widget is initialized inside the __main__
. However you can make use of a QDialog
to achieve the same result:
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QListWidget, QListWidgetItem, QLabel, QApplication, QDialog
class ExampleWidget(QWidget):
def __init__(self):
super().__init__()
listWidget = QListWidget(self)
listWidget.itemDoubleClicked.connect(self.buildExamplePopup)
for n in ["Jack", "Chris", "Joey", "Kim", "Duncan"]:
QListWidgetItem(n, listWidget)
self.setGeometry(100, 100, 100, 100)
self.show()
@pyqtSlot(QListWidgetItem)
def buildExamplePopup(self, item):
exPopup = ExamplePopup(item.text(), self)
exPopup.setGeometry(100, 200, 100, 100)
exPopup.show()
class ExamplePopup(QDialog):
def __init__(self, name, parent=None):
super().__init__(parent)
self.name = name
self.label = QLabel(self.name, self)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = ExampleWidget()
sys.exit(app.exec_())
I also changed a bit your code adding the @pyqtSlot
decorator to the itemDoubleClicked
signal slot handler (you should not declare slots as static).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With