pythonpython-3.xpyqt5qcomboboxpyudev

pyqt5 - How to add value to QComboBox fromfunction


I try to add items to QComboBox (deviceBox in my code) from function "get_devices" using "activated.connect()" method, but QComboBox is empty. Where I was mistaken?

#!/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)

Solution

  • QCombBox object have a method named addItem which could append item to exist item list.

    so you need to modify your code in last line.

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