diff --git a/portcontrol.cpp b/portcontrol.cpp --- a/portcontrol.cpp +++ b/portcontrol.cpp @@ -43,8 +43,16 @@ PortControl::PortControl(QSerialPort* po this, &PortControl::openActionTriggered); portToolBar.addAction(&openAction); - portToolBar.addWidget(&tbPortListBox); - tbPortListBox.setModel(&portList); + portToolBar.addWidget(&tbPortList); + tbPortList.setModel(&portList); + + ui->cbPortList->setModel(&portList); + QObject::connect(ui->cbPortList, + SELECT::OVERLOAD_OF(&QComboBox::activated), + this, &PortControl::onCbPortListActivated); + QObject::connect(&tbPortList, + SELECT::OVERLOAD_OF(&QComboBox::activated), + this, &PortControl::onTbPortListActivated); // setup buttons QObject::connect(ui->pbReloadPorts, &QPushButton::clicked, @@ -121,37 +129,7 @@ PortControl::~PortControl() void PortControl::loadPortList() { - QString currentSelection = ui->cbPortList->currentText(); - - ui->cbPortList->clear(); - - discoveredPorts.clear(); - for (auto port : QSerialPortInfo::availablePorts()) - { - QString pName = port.portName(); - if (!port.description().isEmpty()) pName += QString(" ") + port.description(); - if (port.hasProductIdentifier()) - { - QString vID = QString("%1").arg(port.vendorIdentifier(), 4, 16, QChar('0')); - QString pID = QString("%1").arg(port.productIdentifier(), 4, 16, QChar('0')); - pName = pName + " [" + vID + ":" + pID + "]"; - } - ui->cbPortList->addItem(pName); - discoveredPorts << port.portName(); - } - - ui->cbPortList->addItems(userEnteredPorts); - - // find current selection in the new list, maybe it doesn't exist anymore? - int currentSelectionIndex = ui->cbPortList->findText(currentSelection); - if (currentSelectionIndex >= 0) - { - ui->cbPortList->setCurrentIndex(currentSelectionIndex); - } - else // our port doesn't exist anymore, close port if it's open - { - if (serialPort->isOpen()) togglePort(); - } + portList.loadPortList(); } void PortControl::loadBaudRateList() @@ -306,3 +284,13 @@ void PortControl::openActionTriggered(bo { togglePort(); } + +void PortControl::onCbPortListActivated(int index) +{ + tbPortList.setCurrentIndex(index); +} + +void PortControl::onTbPortListActivated(int index) +{ + ui->cbPortList->setCurrentIndex(index); +}