diff --git a/portcontrol.cpp b/portcontrol.cpp --- a/portcontrol.cpp +++ b/portcontrol.cpp @@ -211,9 +211,16 @@ void PortControl::togglePort() } else { - // port name may contain description - QString portName = ui->cbPortList->currentText().split(" ")[0]; - keepPortName(portName); + // we get the port name from the edit text, which may not be + // in the portList if user hasn't pressed Enter + QString portName = ui->cbPortList->currentText(); + int portIndex = portList.indexOf(portName); + if (portIndex < 0) // not in list, add to model and update the selections + { + portList.appendRow(new PortListItem(portName)); + ui->cbPortList->setCurrentIndex(portList.rowCount()-1); + tbPortList.setCurrentIndex(portList.rowCount()-1); + } serialPort->setPortName(portName); diff --git a/portlist.cpp b/portlist.cpp --- a/portlist.cpp +++ b/portlist.cpp @@ -58,7 +58,7 @@ void PortListItem::construct(QString nam setData(name, PortNameRole); } -QString PortListItem::name() +QString PortListItem::portName() { return data(PortNameRole).toString(); } @@ -78,3 +78,15 @@ void PortList::loadPortList() appendRow(new PortListItem(&portInfo)); } } + +int PortList::indexOf(QString portName) +{ + for (int i = 0; i < rowCount(); i++) + { + if (static_cast(item(i))->portName() == portName) + { + return i; + } + } + return -1; // not found +} diff --git a/portlist.h b/portlist.h --- a/portlist.h +++ b/portlist.h @@ -33,7 +33,7 @@ public: PortListItem(QString name, QString description="", quint16 vid=0, quint16 pid=0); PortListItem(QSerialPortInfo* portInfo); - QString name(); // returns only the port name + QString portName(); // returns only the port name private: // common constructor @@ -46,6 +46,7 @@ public: PortList(QObject* parent=0); void loadPortList(); + int indexOf(QString portName); // return -1 if not found private: QList userEnteredPorts;