更新两种方式

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

View File

@ -9,6 +9,8 @@ class MenuBtnStyle : public QProxyStyle
public:
void drawControl(ControlElement element, const QStyleOption *option,
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