# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-09-04 18:00:21 # Node ID cb8b69a0516c8737ef3476543a43fa4d83e6d76e # Parent 40acd76815dc8381dd49b7660e36af79e8cea225 remember user entered port names on reload diff --git a/portlist.cpp b/portlist.cpp --- a/portlist.cpp +++ b/portlist.cpp @@ -67,16 +67,27 @@ PortList::PortList(QObject* parent) : QStandardItemModel(parent) { loadPortList(); + + // we have to use macro based notation to be able to disconnect + QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), + this, SLOT(onRowsInserted(QModelIndex, int, int))); } void PortList::loadPortList() { clear(); + disconnect(this, SLOT(onRowsInserted(QModelIndex,int,int))); for (auto portInfo : QSerialPortInfo::availablePorts()) { appendRow(new PortListItem(&portInfo)); } + for (auto portName : userEnteredPorts) + { + appendRow(new PortListItem(portName)); + } + QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), + this, SLOT(onRowsInserted(QModelIndex, int, int))); } int PortList::indexOf(QString portName) @@ -90,3 +101,11 @@ int PortList::indexOf(QString portName) } return -1; // not found } + +void PortList::onRowsInserted(QModelIndex parent, int start, int end) +{ + PortListItem* newItem = static_cast(item(start)); + QString portName = newItem->text(); + newItem->setData(portName, PortNameRole); + userEnteredPorts << portName; +} diff --git a/portlist.h b/portlist.h --- a/portlist.h +++ b/portlist.h @@ -17,6 +17,9 @@ along with serialplot. If not, see . */ +#ifndef PORTLIST_H +#define PORTLIST_H + #include #include #include @@ -24,7 +27,7 @@ enum PortListRoles { - PortNameRole = Qt::UserRole // portName as QString + PortNameRole = Qt::UserRole+1 // portName as QString }; class PortListItem : public QStandardItem @@ -42,6 +45,7 @@ private: class PortList : public QStandardItemModel { + Q_OBJECT public: PortList(QObject* parent=0); @@ -49,5 +53,10 @@ public: int indexOf(QString portName); // return -1 if not found private: - QList userEnteredPorts; + QStringList userEnteredPorts; + +private slots: + void onRowsInserted(QModelIndex parent, int start, int end); }; + +#endif // PORTLIST_H