'How to add items to combobox at runtime
Im using Pyqt5 in python and i try to add itmes to combobox at runtime but its not showing unless i restart the program so here is my qestion how to add it dynamiclly
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
item_list=["a","b","c"]
def __init__(self):
super().__init__()
self.setWindowTitle("Title")
self.setGeometry(100, 100, 600, 400)
self.ui_components()
self.show()
def ui_components(self):
combo_box = QComboBox(self)
combo_box.setGeometry(10, 70, 100, 30)
combo_box.currentTextChanged.connect(self.on_changed)
combo_box.addItems(Window.item_list)
add_combobox_button = QPushButton("ADD NEW", self)
add_combobox_button.setGeometry(110, 70, 100, 30)
add_combobox_button.clicked.connect(self.combobox_adding)
def combobox_adding(self):
text, pressed = QInputDialog.getText(self, "New recording", "Name:", QLineEdit.Normal, "")
if pressed:
Window.item_list.append(text)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
