160 void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const override
162 QStyleOptionViewItem options = option;
163 initStyleOption(&options, index);
166 const auto shortcutString = index.data().toString();
169 if (option.state & QStyle::State_Selected) {
170 painter->fillRect(option.rect, option.palette.highlight());
172 painter->fillRect(option.rect, option.palette.base());
175 options.text = QString();
176 options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
178 if (!shortcutString.isEmpty()) {
181 const auto list = [&shortcutString] {
182 auto list = shortcutString.split(QLatin1Char(
'+'), Qt::SkipEmptyParts);
183 if (shortcutString.endsWith(QLatin1String(
"+"))) {
184 list.append(QStringLiteral(
"+"));
188 btns.reserve(list.size());
189 for (
const QString &text : list) {
190 QRect r = option.fontMetrics.boundingRect(text);
191 r.setWidth(r.width() + 8);
192 btns.append({r, text});
195 const auto plusRect = option.fontMetrics.boundingRect(QLatin1Char(
'+'));
198 int x = option.rect.x();
199 const int y = option.rect.y();
200 const int plusY = option.rect.y() + plusRect.height() / 2;
201 const int total = btns.size();
204 painter->translate(QPoint(0, (option.rect.height() - btns.at(0).first.height()) / 2));
207 painter->setRenderHint(QPainter::Antialiasing);
208 for (
const auto &btn : btns) {
209 painter->setPen(Qt::NoPen);
210 const QRect &
rect = btn.first;
212 QRect buttonRect(x, y,
rect.width(),
rect.height());
215 auto shadowRect = buttonRect.translated(0, 1);
216 painter->setBrush(option.palette.shadow());
217 painter->drawRoundedRect(shadowRect, 3, 3);
220 painter->setBrush(option.palette.button());
221 painter->drawRoundedRect(buttonRect, 3, 3);
224 painter->setPen(option.palette.buttonText().color());
225 painter->drawText(buttonRect, Qt::AlignCenter, btn.second);
229 x +=
rect.width() + 5;
230 painter->drawText(QPoint(x, plusY + (
rect.height() / 2)), QString(
"+"));
231 x += plusRect.width() + 5;