在MainWindow下开启
self.centralwidget.setMouseTracking(True)
self.lcc_uart_port.setMouseTracking(True)
self.fluke_uart_port.setMouseTracking(True)
利用combo box的activated信号连接槽函数
self.lcc_uart_port.activated.connect(self.scan_uart_port)
self.fluke_uart_port.activated.connect(self.scan_uart_port)
实现槽函数
def scan_uart_port(self):
new_name_list = []
scan_port_list = []
uart_port_num = QSerialPortInfo.availablePorts()
for info in uart_port_num:
uart_str_name = info.portName().split()
uart_str_desc = info.description().split()
scan_port_list.append(uart_str_name[0] + ' ' + uart_str_desc[0])
if len(scan_port_list) > 0:
for i in scan_port_list:
if i not in new_name_list:
new_name_list.append(i)
else:
scan_port_list = new_name_list
if scan_port_list != self.uart_name_list:
self.uart_name_list = scan_port_list
self.lcc_uart_port.clear()
self.fluke_uart_port.clear()
list_len = len(self.uart_name_list)
for j in range(0, list_len):
self.lcc_uart_port.addItem(self.uart_name_list[j])
self.fluke_uart_port.addItem(self.uart_name_list[j])
这样每次选择combo box的item都会扫描串口了,插拔串口就不用重开软件了。