Is there any way I can create an Input Dialog with a comboBox that does not close after clicking the ok button. I tried using
QInputDialog.setOption(QInputDialog[NoButtons, on=True])
That didn't work. I tried using a second window but couldn't configure it correctly.
Also, is there any way I can use that OK button signal for my logic elsewhere?
If you would like to open a QInputDialog with the option QInputDialog.NoButtons
, you can do this:
dg = QInputDialog()
dg.setOption(QInputDialog.NoButtons)
dg.setComboBoxItems(['Item A', 'Item B', 'Item C'])
dg.exec_()
The purpose of the QInputDialog class is to provide a very simple and convenient way to get user input, without much room for customization. The OK button signal always connects to the dialog's accept
slot. If you want to change the settings for various signals and slots, I would suggest subclassing QDialog and building your own. Here is a simple example where the window will not close when OK is pressed, instead the current item is just printed to the shell.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class CustomDialog(QDialog):
item_selected = pyqtSignal(str)
def __init__(self, items, *args, **kwargs):
super().__init__(*args, **kwargs)
self.box = QComboBox()
self.box.addItems(items)
btn = QPushButton('Ok')
btn.clicked.connect(self.ok_pressed)
vbox = QVBoxLayout(self)
vbox.addWidget(self.box)
vbox.addWidget(btn)
def ok_pressed(self):
self.item_selected.emit(self.box.currentText())
class Template(QWidget):
def __init__(self):
super().__init__()
dg = CustomDialog(['Item A', 'Item B', 'Item C'], self)
dg.item_selected[str].connect(self.do_something)
dg.exec_()
def do_something(self, item):
print(item)
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = Template()
gui.show()
sys.exit(app.exec_())