Search code examples
pythonpyqtpyqt4qcombobox

QComboxBox values didn't change after input data


my QComboBox didn't change/refresh after input data.

whats should i add for refresh the QComboBox

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyClass(object):
    def __init__(self, device_type=None, ip=None, username=None, password=None, secret=None, command=None):
        self.device_type = device_type
        self.ip = ip
        self.username = username
        self.password = password
        self.secret = secret
        self.command = command

device_list = []
ip_list = [] #----- object to load

def addDevice():
    device_type = str(cb_device_list.currentText())
    ip = le_ip.text()
    username = le_username.text()
    password = le_password.text()
    secret = le_enable.text()
    command = 'show tech'
    device_list.append(MyClass(device_type, ip, username, password, secret, command))
    ip_list.append(ip)

##################################
app = QApplication(sys.argv)
app.setStyle('cleanlooks')
window = QWidget()
window.setWindowTitle("Network Automation")
############################# Input IP

# Device Type
lb_device_list = QLabel(window)
lb_device_list.setText('Device Type')
cb_device_list = QComboBox(window)
cb_device_list.addItem('cisco_ios')
cb_device_list.addItem('cisco_s300')

lb_ip = QLabel(window)
bt = QPushButton(window)
btadd = QPushButton(window)

# Ip Device
lb_ip.setText('IP Address')
le_ip = QLineEdit(window)
le_ip.setText('')
le_ip.setPlaceholderText('Input Device IP')
le_ip.setFixedWidth(150)

# username
lb_username = QLabel(window)
le_username = QLineEdit(window)
lb_username.setText('Username')
le_username.setText('')
le_username.setPlaceholderText('Input Username')
le_username.setFixedWidth(150)

# password
lb_password = QLabel(window)
le_password = QLineEdit(window)
lb_password.setText('Password')
le_password.setText('')
le_password.setPlaceholderText('Input Password')
le_password.setFixedWidth(150)

# Privilage Password
lb_enable = QLabel(window)
lb_enable.setText('Privilege Mode Password')
le_enable = QLineEdit(window)
le_enable.setText('')
le_enable.setPlaceholderText('Input Enable Password')
le_enable.setFixedWidth(150)

bt.setText('Generate')
bt.setFixedWidth(70)

btadd.setText('Add')

line = QFrame(window)
line.setFrameShape(QFrame.VLine)
line.setFrameShadow(QFrame.Sunken)
line.setLineWidth(3)

########################### Layout Ip Device List

lb3 = QLabel(window)
lb3.setText('IP Device List')
combobox_ip_list = QComboBox(window)
combobox_ip_list.setFixedWidth(170)
combobox_ip_list.addItems(ip_list)  # Didn't load after input the data

############################## SubLayout and Layout
hblayout = QHBoxLayout()
hblayout.addWidget(bt)
hblayout.addWidget(btadd)

sublayout = QVBoxLayout()
sublayout.addWidget(lb_device_list)
sublayout.addWidget(cb_device_list)
sublayout.addWidget(lb_ip)
sublayout.addWidget(le_ip)
sublayout.addWidget(lb_username)
sublayout.addWidget(le_username)
sublayout.addWidget(lb_password)
sublayout.addWidget(le_password)
sublayout.addWidget(lb_enable)
sublayout.addWidget(le_enable)
sublayout.addLayout(hblayout)

sublayout2 = QVBoxLayout()
sublayout2.addWidget(lb3)
sublayout2.addWidget(combobox_ip_list)
sublayout2.addStretch(1)

layout = QGridLayout(window)
layout.addLayout(sublayout,0,0)
layout.addWidget(line,0,1)
layout.addLayout(sublayout2,0,2)

btadd.clicked.connect(addDevice)

window.show()
sys.exit(app.exec_())

Solution

  • The problem arises because when I use the addItems function it copies the values from the list, it does not save the reference from the list. In your case it is not necessary to save the data in a list, just add it to the QCombobox with the addItem:

    def addDevice():
        device_type = str(cb_device_list.currentText())
        ip = le_ip.text()
        username = le_username.text()
        password = le_password.text()
        secret = le_enable.text()
        command = 'show tech'
        device_list.append(MyClass(device_type, ip, username, password, secret, command))
        #ip_list.append(ip)
        combobox_ip_list.addItem(ip)
    

    complete code:

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    class MyClass(object):
        def __init__(self, device_type=None, ip=None, username=None, password=None, secret=None, command=None):
            self.device_type = device_type
            self.ip = ip
            self.username = username
            self.password = password
            self.secret = secret
            self.command = command
    
    device_list = []
    #ip_list = [] #----- object to load
    
    def addDevice():
        device_type = str(cb_device_list.currentText())
        ip = le_ip.text()
        username = le_username.text()
        password = le_password.text()
        secret = le_enable.text()
        command = 'show tech'
        device_list.append(MyClass(device_type, ip, username, password, secret, command))
        #ip_list.append(ip)
        combobox_ip_list.addItem(ip)
    
    ##################################
    app = QApplication(sys.argv)
    app.setStyle('cleanlooks')
    window = QWidget()
    window.setWindowTitle("Network Automation")
    ############################# Input IP
    
    # Device Type
    lb_device_list = QLabel(window)
    lb_device_list.setText('Device Type')
    cb_device_list = QComboBox(window)
    cb_device_list.addItem('cisco_ios')
    cb_device_list.addItem('cisco_s300')
    
    lb_ip = QLabel(window)
    bt = QPushButton(window)
    btadd = QPushButton(window)
    
    # Ip Device
    lb_ip.setText('IP Address')
    le_ip = QLineEdit(window)
    le_ip.setText('')
    le_ip.setPlaceholderText('Input Device IP')
    le_ip.setFixedWidth(150)
    
    # username
    lb_username = QLabel(window)
    le_username = QLineEdit(window)
    lb_username.setText('Username')
    le_username.setText('')
    le_username.setPlaceholderText('Input Username')
    le_username.setFixedWidth(150)
    
    # password
    lb_password = QLabel(window)
    le_password = QLineEdit(window)
    lb_password.setText('Password')
    le_password.setText('')
    le_password.setPlaceholderText('Input Password')
    le_password.setFixedWidth(150)
    
    # Privilage Password
    lb_enable = QLabel(window)
    lb_enable.setText('Privilege Mode Password')
    le_enable = QLineEdit(window)
    le_enable.setText('')
    le_enable.setPlaceholderText('Input Enable Password')
    le_enable.setFixedWidth(150)
    
    bt.setText('Generate')
    bt.setFixedWidth(70)
    
    btadd.setText('Add')
    
    line = QFrame(window)
    line.setFrameShape(QFrame.VLine)
    line.setFrameShadow(QFrame.Sunken)
    line.setLineWidth(3)
    
    ########################### Layout Ip Device List
    
    lb3 = QLabel(window)
    lb3.setText('IP Device List')
    combobox_ip_list = QComboBox(window)
    combobox_ip_list.setFixedWidth(170)
    #combobox_ip_list.addItems(ip_list)  # Didn't load after input the data
    
    ############################## SubLayout and Layout
    hblayout = QHBoxLayout()
    hblayout.addWidget(bt)
    hblayout.addWidget(btadd)
    
    sublayout = QVBoxLayout()
    sublayout.addWidget(lb_device_list)
    sublayout.addWidget(cb_device_list)
    sublayout.addWidget(lb_ip)
    sublayout.addWidget(le_ip)
    sublayout.addWidget(lb_username)
    sublayout.addWidget(le_username)
    sublayout.addWidget(lb_password)
    sublayout.addWidget(le_password)
    sublayout.addWidget(lb_enable)
    sublayout.addWidget(le_enable)
    sublayout.addLayout(hblayout)
    
    sublayout2 = QVBoxLayout()
    sublayout2.addWidget(lb3)
    sublayout2.addWidget(combobox_ip_list)
    sublayout2.addStretch(1)
    
    layout = QGridLayout(window)
    layout.addLayout(sublayout,0,0)
    layout.addWidget(line,0,1)
    layout.addLayout(sublayout2,0,2)
    
    btadd.clicked.connect(addDevice)
    
    window.show()
    sys.exit(app.exec_())