700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Qt学习笔记:自定义窗体的移动+控件图标

Qt学习笔记:自定义窗体的移动+控件图标

时间:2021-06-08 21:34:58

相关推荐

Qt学习笔记:自定义窗体的移动+控件图标

在这里首先感谢刘大师的作品:Qt编写通用主界面导航(开源)

贴上演示:

本博客主要是研究了刘大师的作品然后自己总结,做点笔记。。不喜勿喷~~~

废话不多说,先贴出代码解析一下:

AppInit::Instance()->start();

单例模式 Instance()

首先说说这个这个Instance(),本人由于是C++新手,对此有好多C++知识不懂。因此在这里记录一下:

这个Instance() 介绍说用于单例模式 : 用来保证系统中只有一个实例。

.h 文件中 class类的定义:

static AppInit *Instance();private:static AppInit *self;

.c文件中

AppInit *AppInit::self = 0;//静态成员变量需要在类体外面进行初始化。AppInit *AppInit::Instance(){if (!self) {QMutex mutex;//保护一个对象,同一时间只能由一个线程进行访问。QMutexLocker locker(&mutex);//加锁if (!self) {self = new AppInit;//创建一个AppInit对象}}return self;}

使用时,采用:

AppInit::Instance()->satrt();

通过这种方式进行类实例的调用,保证单例模式的进行。

(2). 自定义窗体的移动:

在main 函数中,首先调用这句话,

AppInit::Instance()->start();

执行单例模式,并且加载事件过滤器。

然后在新建的窗体Widget构造函数中,调用以下函数,这里设置窗体属性(property)是为了对应qApp中加载的事件过滤器。使其能够实现窗体的移动。

this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);//去除标题栏this->setProperty("canMove", true);//将对象的名称属性的设置为canMove。 可以移动

在AppInit .c文件中:

//加载事件过滤器void AppInit::start(){qApp->installEventFilter(this);}//重写事件过滤器bool AppInit::eventFilter(QObject *obj, QEvent *evt)//参数:对象,事件{QWidget *w = (QWidget *)obj;//强制转换为QWidgetif (!w->property("canMove").toBool())//得到QWidget的属性是canMove(可以移动)。{return QObject::eventFilter(obj, evt);}static QPoint mousePoint;//静态变量 --> 鼠标坐标static bool mousePressed = false;QMouseEvent *event = static_cast<QMouseEvent *>(evt);if (event->type() == QEvent::MouseButtonPress)//事件类型 鼠标按钮按下{if (event->button() == Qt::LeftButton) //左键{mousePressed = true;mousePoint = event->globalPos() - w->pos();return true;}}else if (event->type() == QEvent::MouseButtonRelease) //事件类型 鼠标按钮释放{mousePressed = false;return true;}else if (event->type() == QEvent::MouseMove)//事件类型 鼠标移动{if (mousePressed && (event->buttons() && Qt::LeftButton)){w->move(event->globalPos() - mousePoint);//窗体移动坐标return true;}}return QObject::eventFilter(obj, evt);}

(3)控件图标:

这里的控件图标实现,主要是采用:fontawesome图标

fontawesome是一个图标的集合,里面有好多的图标,使用起来也还是非常方便的。

图标信息可以到官网去查:http://fontawesome.io/cheatsheet/

fontawesome-webfont.ttf 下载地址:/s/1sjyvp3v

具体的实现重要是通过调用以下函数:

QPushButton * Btn = new QPushButton(widget);IconHelper::Instance()->SetIcon(Btn, QChar(0xf192), 12);

其中的QChar(0xf192) 是fontawesome是图标集合中一个图标对应的uncode编码。使用不同的uncode编码即对应了不同的图标。

这里还可以的根据指定的绘制得到需要的pixmap

//依据 宽度,高度,大小,图标uncode十六进制编码,颜色 绘制图片QPixmap IconHelper::getPixmap(const QString &color, QChar c, quint32 size,quint32 pixWidth, quint32 pixHeight){QPixmap pix(pixWidth, pixHeight);//定义一个对象pix.fill(Qt::transparent);//透明的黑色值(即,QColor(0,0,0,0))填充QPainter painter;//定义绘图对象painter.begin(&pix);//调用begin时,所有的Pen Brush 重置为默认值。//设置给定的绚染提示: 抗锯齿+ 抗锯齿文本painter.setRenderHints(QPainter::Antialiasing |QPainter::TextAntialiasing);painter.setPen(QColor(color));painter.setBrush(QColor(color));iconFont.setPointSize(size);painter.setFont(iconFont);//设置字体painter.drawText(pix.rect(), Qt::AlignCenter, c);//画图标 设置文本中央对齐,painter.end();//结束绘画。 绘画时使用的任何资源都被释放。return pix;}

通过调用:

QPixmap pix = IconHelper::Instance()->getPixmap(listColorText.at(i), listChar.at(i), iconSize, iconWidth, iconHeight);//根据指定绘制图标样式btn->setIcon(QIcon(pix));//设置图标

项目代码在我的github仓库:

/xiedonghuilove/Package/tree/master/QFreamWork/uidemo18

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。