I\'m working on a Qt application (in C++). Without appyling any styles, my menu looks like this:
I\'d like it to look like this:
How do I a
One way to solve the problem is to use QProxyStyle:
customstyle.h
#ifndef CUSTOMSTYLE_H
#define CUSTOMSTYLE_H
#include
#include
class CustomStyle : public QProxyStyle{
public:
using QProxyStyle::QProxyStyle;
void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const override
{
if(element == QStyle::CE_MenuItem){
QStyleOptionMenuItem myMenuOption;
const QStyleOptionMenuItem *menuOption =
qstyleoption_cast(opt);
if (menuOption) {
const int width = pixelMetric(PM_SmallIconSize)+6;
myMenuOption = *menuOption;
QRect r(myMenuOption.rect);
r.setLeft(-width);
myMenuOption.rect = r;
}
QProxyStyle::drawControl(element, &myMenuOption, p, w);
return;
}
QProxyStyle::drawControl(element, opt, p, w);
}
};
#endif // CUSTOMSTYLE_H
then you install it in the QApplication
:
QApplication a(argc, argv);
QApplication::setStyle(new CustomStyle);