summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ui/qt/percent_bar_delegate.cpp9
-rw-r--r--ui/qt/percent_bar_delegate.h21
2 files changed, 29 insertions, 1 deletions
diff --git a/ui/qt/percent_bar_delegate.cpp b/ui/qt/percent_bar_delegate.cpp
index 06ee6f422c..ae763d2462 100644
--- a/ui/qt/percent_bar_delegate.cpp
+++ b/ui/qt/percent_bar_delegate.cpp
@@ -34,10 +34,17 @@ void PercentBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
{
QStyleOptionViewItemV4 optv4 = option;
QStyledItemDelegate::initStyleOption(&optv4, index);
- double value = index.data(Qt::UserRole).toDouble();
QStyledItemDelegate::paint(painter, option, index);
+ bool ok = false;
+ double value = index.data(Qt::UserRole).toDouble(&ok);
+
+ if (!ok || !index.data(Qt::DisplayRole).toString().isEmpty()) {
+ // We don't have a valid value or the item has visible text.
+ return;
+ }
+
painter->save();
if (QApplication::style()->objectName().contains("vista")) {
diff --git a/ui/qt/percent_bar_delegate.h b/ui/qt/percent_bar_delegate.h
index 4bd2473e86..0a3ac9af67 100644
--- a/ui/qt/percent_bar_delegate.h
+++ b/ui/qt/percent_bar_delegate.h
@@ -22,6 +22,27 @@
#ifndef PERCENTBARDELEGATE_H
#define PERCENTBARDELEGATE_H
+/*
+ * @file Percent bar delegate.
+ *
+ * QStyledItemDelegate subclass that will draw a percentage value and a
+ * single-item bar chart for the specified value.
+ *
+ * This is intended to be used in QTreeWidgets to show percentage values.
+ * To use it, first call setItemDelegate:
+ *
+ * myTreeWidget()->setItemDelegateForColumn(col_pct_, new PercentBarDelegate());
+ *
+ * Then, for each QTreeWidgetItem, set a double value using setData:
+ *
+ * setData(col_pct_, Qt::UserRole, QVariant::fromValue<double>(packets_ * 100.0 / num_packets));
+ *
+ * If the item data cannot be converted to a valid double value or if its
+ * text string is non-empty then it will be rendered normally (i.e. the
+ * percent text and bar will not be drawn). This lets you mix normal and
+ * percent bar rendering between rows.
+ */
+
#include <QStyledItemDelegate>
class PercentBarDelegate : public QStyledItemDelegate