0

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)

1 Answers1

0

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)

HolaYang
  • 419
  • 2
  • 10
  • No. deviceBox just empty. – Aleksey Samoilov May 17 '17 at 06:47
  • Oh sorry, key to the problem is that, `activated` signal will sent if you **choose** a item in it. But your item list is always empty. So you need to connect another event that you could active with `get_device`, like `another_push_button.clicked` – HolaYang May 17 '17 at 07:03
  • With QPushButton it works fine, but how let it works without it? – Aleksey Samoilov May 17 '17 at 07:30
  • QComboBox has no clicked signal, So if you want to achieve it, initialize it with a empty item. when you clicked, it choose a empty item and send activated signal to the target method `get_device`, maybe it works. Oh, remembering remove the first empty item. – HolaYang May 17 '17 at 07:35