pyqt5 - 如何从函数中为QComboBox添加值

时间:2017-05-17 06:19:30

标签: python python-3.x pyqt5 qcombobox pyudev

我尝试使用“activated.connect()”方法从函数“get_devices”向QComboBox(我的代码中的deviceBox)添加项目,但QComboBox为空。哪里弄错了?

#!/usr/bin/env python3

from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialog
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from mainwindow import *
import sys
import pyudev


class MainProg (QMainWindow, Ui_MainWindow):

    file_open = pyqtSignal(str, str)

    def  __init__(self,  window):
        QMainWindow.__init__(self)
        self.setupUi(window)

        self.openisoButton.clicked.connect(self.openISO)
        self.aboutButton.clicked.connect(self.about)
        self.deviceBox.activated.connect(self.get_devices)

    def get_devices(self):
        devices = []
        context = pyudev.Context()
        for device in context.list_devices(subsystem='block', ID_BUS="usb"):
            devices.append(str(device['DEVNAME']))

        self.deviceBox.addItems(devices)

1 个答案:

答案 0 :(得分:0)

QCombBox对象有一个名为addItem的方法,它可以将项目附加到现有项目列表中。

所以你需要在最后一行修改你的代码。

for device in devices: self.deviceBox.addItem(device)