38 #include <QApplication>
41 #include <QStandardItemModel>
55 setEditTriggers(QAbstractItemView::NoEditTriggers);
60 setFrameShape(QFrame::StyledPanel);
61 setFrameShadow(QFrame::Raised);
68 setMinimumHeight(300);
70 verticalScrollBar()->setPageStep(3);
71 verticalScrollBar()->setSingleStep(1);
73 model_ =
new QStandardItemModel(
this);
82 for (
const QString& nav : navs)
84 QStandardItem* item =
new QStandardItem();
85 item->setData(QVariant::fromValue(nav), Qt::DisplayRole);
86 item->setFlags(Qt::NoItemFlags);
87 model_->appendRow(item);
96 model_->item(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
97 Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
99 model_->item(index)->setFlags(Qt::NoItemFlags);
108 QModelIndex top = model_->index(index, 0, QModelIndex());
109 QModelIndex bottom = model_->index(index, 0, QModelIndex());
111 QItemSelection selection(top, bottom);
112 selectionModel()->reset();
113 selectionModel()->select(selection, QItemSelectionModel::Select);
118 return model_->item(index)->flags() > Qt::NoItemFlags;
131 return QSize(option.rect.width(), 45);
134 void NavDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index)
const
136 const bool is_selected = option.state & QStyle::State_Selected;
137 const QPalette& palette = QApplication::palette();
139 QString nav_name = displayText(index.data(), option.locale);
144 QLinearGradient background_gradient(option.rect.topLeft(), option.rect.bottomLeft());
147 background_gradient.setColorAt(0, palette.color(QPalette::Highlight).lighter(125));
148 background_gradient.setColorAt(1, palette.color(QPalette::Highlight));
149 painter->fillRect(option.rect, QBrush(background_gradient));
153 background_gradient.setColorAt(0, palette.color(QPalette::Light));
154 background_gradient.setColorAt(1, palette.color(QPalette::Light).darker(105));
155 painter->fillRect(option.rect, QBrush(background_gradient));
161 painter->drawLine(option.rect.topLeft(), option.rect.topRight());
162 painter->setPen(palette.color(QPalette::Light));
163 const QPoint offset(0, 1);
164 painter->drawLine(option.rect.topLeft() + offset, option.rect.topRight() + offset);
167 QRect text_rect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 10, option.rect.height());
168 QFont text_font(painter->font());
169 text_font.setPixelSize(14);
170 painter->setFont(text_font);
174 painter->setPen(palette.color(QPalette::HighlightedText));
175 else if (!option.state.testFlag(QStyle::State_Enabled))
176 painter->setPen(palette.color(QPalette::Dark));
178 painter->setPen(palette.color(QPalette::ButtonText));
180 painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, nav_name);
NavDelegate(QObject *parent=nullptr)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override