QStyle(4):StandardPixmap和StyleHint枚举
本文为原创文章,转载请注明出处,或注明转载自“黄邦勇帅(原名:黄勇)
本文出自本人原创著作《Qt5.10 GUI完全参考手册》网盘地址:
https://pan.baidu.com/s/1iqagt4SEC8PUYx6t3ku39Q
《C++语法详解》网盘地址:https://pan.baidu.com/s/1dIxLMN5b91zpJN2sZv1MNg
若对C++语法不熟悉,建议参阅本人所著《C++语法详解》一书,电子工业出版社出版,该书语法示例短小精悍,对查阅C++知识点相当方便,并对语法原理进行了透彻、深入详细的讲解,可确保读者彻底弄懂C++的原理,彻底解惑C++,使其知其然更知其所以然。此书是一本全面了解C++不可多得的案头必备图书。
QStyle::StandardPixmap枚举及相关成员函数如下
virtual QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *option = Q_NULLPTR, const QWidget *widget = Q_NULLPTR) const = 0; //纯虚函数
返回standardIcon指定的标准图标,QStyle::StandardPixmap枚举见表13-13(完整取值请查阅帮助文档)
此枚举描述了可用的标准像素图,与函数standardIcon()有关
示例13.8:使用自定义的图标(重新实现standardIcon()函数)(效果见图13-11)
//m.h文件的内容
#ifndef M_H
#define M_H
#include<QtWidgets>
class B:public QProxyStyle{ Q_OBJECT
public:B(){}
QIcon standardIcon(StandardPixmap ic, const QStyleOption *op = Q_NULLPTR,
const QWidget *w = Q_NULLPTR) const{
qDebug()<<ic;
//若是询问消息框,则使用图像1i.png作为其图标
if(ic==QStyle::SP_MessageBoxQuestion) return QIcon("F:/1i.png");
return QProxyStyle::standardIcon(ic,op,w); }};
#endif // M_H
//m.cpp文件的内容
#include "m.h"
int main(int argc, char *argv[]){
QApplication aa(argc,argv);
QMessageBox ms(QMessageBox::Question,"AAA","BBB");
ms.setModal(0); ms.setStyle(new B());
ms.show(); return aa.exec(); }
13.5.3 QStyle::StyleHint枚举及相关成员函数如下
virtual int styleHint(StyleHint hint, const QStyleOption *option = Q_NULLPTR, const QWidget *widget=Q_NULLPTR,QStyleHintReturn *returnData=Q_NULLPTR)const=0; //纯虚函数
返回由hint指示的样式提示,QStyle::StyleHint枚举比较多,限于篇幅本文仅讲解怎样使用该枚举(以滚动条为例)以及怎样重新实现styleHint()函数,
完整的QStyle::StyleHint枚举值可参阅帮助文档,表13-14是其中的一部分枚举成员
示例13.9:使用样式提示(重新实现styleHint()函数)(效果见图13-12)
//m.h文件的内容
#ifndef M_H
#define M_H
#include<QtWidgets>
class B:public QProxyStyle{ Q_OBJECT
public:B(){}
int styleHint(StyleHint h, const QStyleOption *op = Q_NULLPTR,
const QWidget *w = Q_NULLPTR, QStyleHintReturn *rd = Q_NULLPTR) const{
qDebug()<<h;
switch(h){ //以下设置的效果见图13-12的讲解
case QStyle::SH_ScrollBar_LeftClickAbsolutePosition: return true;
case QStyle::SH_ScrollBar_MiddleClickAbsolutePosition:return true;
case QStyle::SH_ScrollBar_ScrollWhenPointerLeavesControl: return false; }
return QProxyStyle::styleHint(h,op,w); }};
//m.cpp文件的内容
#include "m.h"
int main(int argc, char *argv[]){ QApplication aa(argc,argv);
QWidget w; QScrollBar *ps=new QScrollBar(&w);
ps->resize(22,111); ps->move(55,55); ps->setStyle(new B()); //使用自定义样式
w.resize(444,333); w.show(); return aa.exec(); }
13.5.4 其他枚举及相关成员函数
virtual QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget = Q_NULLPTR) const = 0; //纯虚函数
返回由type指定的元素的大小,枚举contentsType见表13-15
扫描二维码关注公众号,回复:
13443030 查看本文章
本文作者:黄邦勇帅(原名:黄勇)