更新两种方式

This commit is contained in:
metanoia1989 2020-10-24 14:47:46 +08:00
parent 86337b103f
commit 3aed340f11
2 changed files with 74 additions and 66 deletions

View File

@ -3,6 +3,9 @@
#include <QPainter> #include <QPainter>
#include<QDebug> #include<QDebug>
/**
* Python版改的Python写起来比较快比较方便
*/
void MenuBtnStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, void MenuBtnStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const QPainter *painter, const QWidget *widget) const
{ {
@ -40,80 +43,83 @@ void MenuBtnStyle::drawControl(QStyle::ControlElement element, const QStyleOptio
} }
//void MenuBtnStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, /**
// QPainter *painter, const QWidget *widget) const * stackoverflow上的回答的
//{ */
// if(element==CE_PushButtonLabel) void MenuBtnStyle::drawControl2(QStyle::ControlElement element, const QStyleOption *option,
// { QPainter *painter, const QWidget *widget) const
// if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) {
// { if(element==CE_PushButtonLabel)
// QRect textRect = button->rect; {
// uint tf = Qt::AlignVCenter | Qt::TextShowMnemonic; if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option))
// if (!proxy()->styleHint(SH_UnderlineShortcut, button, widget)) {
// tf |= Qt::TextHideMnemonic; QRect textRect = button->rect;
uint tf = Qt::AlignVCenter | Qt::TextShowMnemonic;
if (!proxy()->styleHint(SH_UnderlineShortcut, button, widget))
tf |= Qt::TextHideMnemonic;
// if (!button->icon.isNull()) { if (!button->icon.isNull()) {
// QRect iconRect; QRect iconRect;
// QIcon::Mode mode = button->state & State_Enabled ? QIcon::Normal : QIcon::Disabled; QIcon::Mode mode = button->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
// if (mode == QIcon::Normal && button->state & State_HasFocus) if (mode == QIcon::Normal && button->state & State_HasFocus)
// mode = QIcon::Active; mode = QIcon::Active;
// QIcon::State state = QIcon::Off; QIcon::State state = QIcon::Off;
// if (button->state & State_On) if (button->state & State_On)
// state = QIcon::On; state = QIcon::On;
// QWindow *window = widget == nullptr ? nullptr : widget->window()->windowHandle(); QWindow *window = widget == nullptr ? nullptr : widget->window()->windowHandle();
// QPixmap pixmap = button->icon.pixmap(window, button->iconSize, mode, state); QPixmap pixmap = button->icon.pixmap(window, button->iconSize, mode, state);
// int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio(); int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio();
// int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio(); int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio();
// int labelWidth = pixmapWidth; int labelWidth = pixmapWidth;
// int labelHeight = pixmapHeight; int labelHeight = pixmapHeight;
// int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint() int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint()
// int textWidth = button->fontMetrics.boundingRect(option->rect, tf, button->text).width(); int textWidth = button->fontMetrics.boundingRect(option->rect, tf, button->text).width();
// if (!button->text.isEmpty()) if (!button->text.isEmpty())
// labelWidth += (textWidth + iconSpacing); labelWidth += (textWidth + iconSpacing);
// /*************************************************************/ /*************************************************************/
// // Make the icon rectangle always be 10px in from the left edge // Make the icon rectangle always be 10px in from the left edge
// /*************************************************************/ /*************************************************************/
// iconRect = QRect(10, iconRect = QRect(10,
// textRect.y() + (textRect.height() - labelHeight) / 2, textRect.y() + (textRect.height() - labelHeight) / 2,
// pixmapWidth, pixmapHeight); pixmapWidth, pixmapHeight);
// iconRect = visualRect(button->direction, textRect, iconRect); iconRect = visualRect(button->direction, textRect, iconRect);
// /***********************************/ /***********************************/
// // Always horizontal align the text // Always horizontal align the text
// /***********************************/ /***********************************/
// tf |= Qt::AlignHCenter; tf |= Qt::AlignHCenter;
// if (button->state & (State_On | State_Sunken)) if (button->state & (State_On | State_Sunken))
// iconRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, option, widget), iconRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, option, widget),
// proxy()->pixelMetric(PM_ButtonShiftVertical, option, widget)); proxy()->pixelMetric(PM_ButtonShiftVertical, option, widget));
// painter->drawPixmap(iconRect, pixmap); painter->drawPixmap(iconRect, pixmap);
// } else { } else {
// tf |= Qt::AlignHCenter; tf |= Qt::AlignHCenter;
// } }
// if (button->state & (State_On | State_Sunken)) if (button->state & (State_On | State_Sunken))
// textRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, option, widget), textRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, option, widget),
// proxy()->pixelMetric(PM_ButtonShiftVertical, option, widget)); proxy()->pixelMetric(PM_ButtonShiftVertical, option, widget));
// if (button->features & QStyleOptionButton::HasMenu) { if (button->features & QStyleOptionButton::HasMenu) {
// int indicatorSize = proxy()->pixelMetric(PM_MenuButtonIndicator, button, widget); int indicatorSize = proxy()->pixelMetric(PM_MenuButtonIndicator, button, widget);
// if (button->direction == Qt::LeftToRight) if (button->direction == Qt::LeftToRight)
// textRect = textRect.adjusted(0, 0, -indicatorSize, 0); textRect = textRect.adjusted(0, 0, -indicatorSize, 0);
// else else
// textRect = textRect.adjusted(indicatorSize, 0, 0, 0); textRect = textRect.adjusted(indicatorSize, 0, 0, 0);
// } }
// proxy()->drawItemText(painter, textRect, tf, button->palette, (button->state & State_Enabled), proxy()->drawItemText(painter, textRect, tf, button->palette, (button->state & State_Enabled),
// button->text, QPalette::ButtonText); button->text, QPalette::ButtonText);
// } }
// qDebug() << "调用了吗???"; qDebug() << "调用了吗???";
// return; return;
// } }
// // For all other controls, draw the default // For all other controls, draw the default
// QProxyStyle::drawControl(element, option, painter, widget); QProxyStyle::drawControl(element, option, painter, widget);
//} }

View File

@ -9,6 +9,8 @@ class MenuBtnStyle : public QProxyStyle
public: public:
void drawControl(ControlElement element, const QStyleOption *option, void drawControl(ControlElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget = nullptr) const override; QPainter *painter, const QWidget *widget = nullptr) const override;
void drawControl2(ControlElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget = nullptr) const;
}; };
#endif // MENUBTNSTYLE_H #endif // MENUBTNSTYLE_H