Issue
I want to create a modal dialog in PyQt, which contains a drop-down button. Here is what I've tried:
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QPushButton, QMenu, QApplication, QMainWindow, QLabel, QWidget
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("MainWindow")
btn = QPushButton("Open FileDialog")
btn.clicked.connect(self.openFileDialog)
vbox = QVBoxLayout()
vbox.addWidget(btn)
self.setLayout(vbox)
def openFileDialog(self):
file_dialog = FileDialog(self)
file_dialog.exec()
class FileDialog(QDialog):
def __init__(self, parent: QWidget):
super().__init__(parent)
menu = QMenu()
menu.addAction("Open")
menu.addAction("Save")
btn = QPushButton("More")
btn.setMenu(menu)
vbox = QVBoxLayout()
vbox.addWidget(btn)
self.setLayout(vbox)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
The drop-down button doesn't work, I can click on it, it shows two options, but I can neither select nor click on any of them.
Is there any way to solve the problem?
Solution
Thanks to the comments by @musicamante and @ekhumoro, I changed menu = QMenu()
to menu = QMenu(self)
, then the problem got solved.
Answered By - Searene
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.