QComboxBox 值在输入数据后没有改变

QComboxBox values didn't change after input data

我的QComboBox输入数据后没有change/refresh。

我应该添加什么来刷新 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_())

问题的出现是因为当我使用 addItems 函数时它从列表中复制值,它不保存列表中的引用。在您的情况下,没有必要将数据保存在列表中,只需将其添加到 QComboboxaddItem:

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)

完整代码:

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_())