Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

watchlist: sortable column headers with numeric sort role

Abhi Kakadiya4e50d2c53afa
diff --git a/fincept-qt/src/screens/watchlist/WatchlistScreen.cpp b/fincept-qt/src/screens/watchlist/WatchlistScreen.cpp
index 74c9d6f1b..b733134aa 100644
--- a/fincept-qt/src/screens/watchlist/WatchlistScreen.cpp
+++ b/fincept-qt/src/screens/watchlist/WatchlistScreen.cpp
@@ -500,10 +500,12 @@ void WatchlistScreen::rebuild_from_cache() {
}
if (quotes.isEmpty()) {
// No data yet — show placeholder rows.
+ table_->setSortingEnabled(false);
table_->clear_data();
for (const auto& s : stocks_) {
table_->add_row({s.symbol, s.name, "--", "--", "--", "--", "--", "--"});
}
+ table_->setSortingEnabled(true);
return;
}
populate_table(quotes);
@@ -553,6 +555,9 @@ void WatchlistScreen::hub_unsubscribe_all() {
void WatchlistScreen::populate_table(const QVector<services::QuoteData>& quotes) {
+ // Disable sorting during population to prevent per-row re-sorting
+ // (avoids both visual flickering and O(n log n) overhead per insert).
+ table_->setSortingEnabled(false);
table_->clear_data();
// Build a map for quick lookup
@@ -565,13 +570,25 @@ void WatchlistScreen::populate_table(const QVector<services::QuoteData>& quotes)
auto it = quote_map.find(s.symbol);
if (it != quote_map.end()) {
const auto& q = it.value();
- table_->add_row({q.symbol, q.name.isEmpty() ? s.name : q.name, QString("$%1").arg(q.price, 0, 'f', 2),
+ table_->add_row({q.symbol, q.name.isEmpty() ? s.name : q.name,
+ QString("$%1").arg(q.price, 0, 'f', 2),
QString("%1%2").arg(q.change >= 0 ? "+" : "").arg(q.change, 0, 'f', 2),
QString("%1%2%").arg(q.change_pct >= 0 ? "+" : "").arg(q.change_pct, 0, 'f', 2),
- QString("$%1").arg(q.high, 0, 'f', 2), QString("$%1").arg(q.low, 0, 'f', 2),
+ QString("$%1").arg(q.high, 0, 'f', 2),
+ QString("$%1").arg(q.low, 0, 'f', 2),
fincept::ui::formatting::format_compact_volume(static_cast<qint64>(q.volume))});
int row = table_->rowCount() - 1;
+
+ // Stamp numeric EditRole values so Qt sorts by magnitude,
+ // not by the display string ("$2.5M" vs "$999K" etc.).
+ table_->set_cell_numeric(row, 2, q.price); // PRICE
+ table_->set_cell_numeric(row, 3, q.change); // CHANGE
+ table_->set_cell_numeric(row, 4, q.change_pct); // CHG %
+ table_->set_cell_numeric(row, 5, q.high); // HIGH
+ table_->set_cell_numeric(row, 6, q.low); // LOW
+ table_->set_cell_numeric(row, 7, q.volume); // VOLUME
+
// Green = good, Red = bad
QString chg_color = q.change_pct >= 0 ? colors::POSITIVE : colors::NEGATIVE;
table_->set_cell_color(row, 3, chg_color);
@@ -580,6 +597,9 @@ void WatchlistScreen::populate_table(const QVector<services::QuoteData>& quotes)
table_->add_row({s.symbol, s.name, "--", "--", "--", "--", "--", "--"});
}
}
+
+ // Re-enable sorting — Qt will apply the current sort column/order once.
+ table_->setSortingEnabled(true);
}
// ── Slots ────────────────────────────────────────────────────────────────────
diff --git a/fincept-qt/src/ui/tables/DataTable.cpp b/fincept-qt/src/ui/tables/DataTable.cpp
index 7dabe8ffa..fcd92a73a 100644
--- a/fincept-qt/src/ui/tables/DataTable.cpp
+++ b/fincept-qt/src/ui/tables/DataTable.cpp
@@ -15,6 +15,11 @@ DataTable::DataTable(QWidget* parent) : QTableWidget(parent) {
verticalHeader()->setVisible(false);
horizontalHeader()->setStretchLastSection(true);
horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
+ // NOTE: sorting is intentionally NOT enabled here.
+ // Callers that need sorting (e.g. WatchlistScreen) opt in with
+ // setSortingEnabled(true) after attaching numeric EditRole values.
+ // Enabling it globally causes lexicographic sort on tables that only
+ // use setText() — e.g. "$10" sorts before "$2", "100K" before "2M".
setStyleSheet(QString("QTableWidget { background: %1; alternate-background-color: %2; "
"gridline-color: %3; border: none; }"
"QTableWidget::item { padding: 4px 8px; height: 26px; }"
@@ -61,4 +66,10 @@ void DataTable::set_cell_color(int row, int col, const QString& color) {
it->setForeground(QColor(color));
}
+void DataTable::set_cell_numeric(int row, int col, double value) {
+ auto* it = item(row, col);
+ if (it)
+ it->setData(Qt::EditRole, value);
+}
+
} // namespace fincept::ui
diff --git a/fincept-qt/src/ui/tables/DataTable.h b/fincept-qt/src/ui/tables/DataTable.h
index dc5fcd218..81df89fb7 100644
--- a/fincept-qt/src/ui/tables/DataTable.h
+++ b/fincept-qt/src/ui/tables/DataTable.h
@@ -19,6 +19,11 @@ class DataTable : public QTableWidget {
// Color a specific cell
void set_cell_color(int row, int col, const QString& color);
+
+ /// Set a numeric sort key on a cell without changing its display text.
+ /// Call after add_row() for columns that should sort numerically
+ /// (price, change, volume, etc.).
+ void set_cell_numeric(int row, int col, double value);
};
} // namespace fincept::ui