Skip to content

Commit

Permalink
qt: Handle peer addition/removal in a right way
Browse files Browse the repository at this point in the history
This change fixes a bug when a multiple rows selection gets inconsistent
after a peer addition/removal.
  • Loading branch information
hebasto committed Jan 27, 2021
1 parent 3746548 commit 32ce58c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/qt/peertablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,30 @@ void PeerTableModel::refresh()
new_peers_data.append(stats);
}

Q_EMIT layoutAboutToBeChanged();
m_peers_data.swap(new_peers_data);
Q_EMIT layoutChanged();
// Handle a new peer addition or an old peer in a right way. See:
// - https://doc.qt.io/qt-5/model-view-programming.html#inserting-and-removing-rows
// - https://doc.qt.io/qt-5/model-view-programming.html#resizable-models
// We use the fact that interfaces::Node::getNodesStats
// returns std::vector naturally sorted by nodeid.
for (int i = 0; i < m_peers_data.size();) {
if (i < new_peers_data.size() && m_peers_data.at(i).nodeStats.nodeid == new_peers_data.at(i).nodeStats.nodeid) {
++i;
continue;
}
// A peer has been removed from the table.
beginRemoveRows(QModelIndex(), i, i);
m_peers_data.erase(m_peers_data.begin() + i);
endRemoveRows();
}

if (m_peers_data.size() < new_peers_data.size()) {
// Some peers have been added to the end of the table.
beginInsertRows(QModelIndex(), m_peers_data.size(), new_peers_data.size() - 1);
m_peers_data.swap(new_peers_data);
endInsertRows();
} else {
m_peers_data.swap(new_peers_data);
}

Q_EMIT changed();
}
3 changes: 3 additions & 0 deletions src/qt/peertablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class PeerTableModel : public QAbstractTableModel
public Q_SLOTS:
void refresh();

Q_SIGNALS:
void changed();

private:
//! Internal peer data structure.
QList<CNodeCombinedStats> m_peers_data{};
Expand Down
2 changes: 1 addition & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ void RPCConsole::setClientModel(ClientModel *model, int bestblock_height, int64_

// peer table signal handling - update peer details when selecting new node
connect(ui->peerWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RPCConsole::updateDetailWidget);
connect(model->getPeerTableModel(), &PeerTableModel::layoutChanged, this, &RPCConsole::updateDetailWidget);
connect(model->getPeerTableModel(), &PeerTableModel::changed, this, &RPCConsole::updateDetailWidget);

// set up ban table
ui->banlistWidget->setModel(model->getBanTableModel());
Expand Down

0 comments on commit 32ce58c

Please sign in to comment.