Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh items in Qcombobox [python]?

Tags:

python

pyqt

I have a list of data which is shown in the Qcombobox (using 'addItems') Now - if the list is changed by loading data from a file with pushbutton - i don't see the new data in the combobx. The new data is there (I can print it after loading) What do I miss? below is the code for the simplified gui

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
import pandas as pd

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def __init__(self):
        self.comboData=['None']

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(456, 172)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))

        self.comboBox = QtGui.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(120, 60, 69, 22))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.setEditable(True)
        self.comboBox.addItems(self.comboData)

        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(260, 60, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.clicked.connect(self.load)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 456, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "Load", None))

    def load(self):
        fileName=QFileDialog.getOpenFileName(MainWindow,'Load File')
        data_pd=pd.read_csv(fileName,index_col=0,na_filter=False)
        self.comboData=list(data_pd.var1)
        print(self.comboData)



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
like image 749
Shuki Avatar asked Aug 29 '26 22:08

Shuki


1 Answers

you only load the data to self.comboData but do not load them to self.comboBox. At the end of

def load(self):

add the following lines:

self.comboBox.clear()       # delete all items from comboBox
self.comboBox.addItems(self.comboData) # add the actual content of self.comboData

As the model used by QComboBox emits the dataChanged() signal whenever the data in an item are changed, the combobox is repainted automatically and it is not necessary to update the combobox by

self.comboBox.update()
like image 162
a_manthey_67 Avatar answered Sep 01 '26 12:09

a_manthey_67



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!