自定义QMenu图标大小


上代码:

///////////////////////////////////////////////////////////
// QMenu的大图标定义
///////////////////////////////////////////////////////////

#ifndef CUSTOMICONSTYLE_H
#define CUSTOMICONSTYLE_H

#include <QCommonStyle>

class CustomIconStyle : public QCommonStyle
{
	Q_OBJECT

public:
	CustomIconStyle();
	~CustomIconStyle();

	void SetCustomSize(int);

	//protected:
	virtual int pixelMetric(PixelMetric metric, const QStyleOption * option, const QWidget * widget) const;

private:
	int mSize;
};

#endif // CUSTOMICONSTYLE_H

#include "CustomIconStyle.h"

CustomIconStyle::CustomIconStyle()
{
	mSize = 20;
}

CustomIconStyle::~CustomIconStyle()
{

}


void CustomIconStyle::SetCustomSize( int nSize )
{
	mSize = nSize;
}

int CustomIconStyle::pixelMetric( PixelMetric metric, const QStyleOption * option, const QWidget * widget ) const
{
	int s = QCommonStyle::pixelMetric(metric, option, widget);
	if (metric == QStyle::PM_SmallIconSize) {
		s = mSize;
	}

	return s;
}

调用方法:

CustomIconStyle* pStyle = new CustomIconStyle;
	mpMikeMenu->setStyle(pStyle);


你可能感兴趣的:(图标,QMenu)