QT 带箭头的控件QPolygon

embedded/2024/9/25 20:51:03/

由于对当前项目需要绘制一个箭头控件,所以使用了QPainter和QPolygon来进行绘制,原理就是计算填充,下面贴出代码和效果图

这里简单介绍下QPolygon
QPolygon是继承自

QVector<QPoint>

那么可以很简单的理解为,他就是一个点的集合
所以由3个点就构成了一个箭头,当然更复杂的箭头大家可以自己去进行构建,由于我的项目需要的只是单纯箭头就展现如下代码,我还填充了一个矩形框作为背景。

void arrowWidget::paintEvent(QPaintEvent* event)
{QPainter painter(this);painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);int cornerRadius = qMin(this->width(), this->height()) / 10; QRect rect(10, 10, this->width() - 20, this->height() - 20); painter.setPen(Qt::NoPen);painter.setBrush(QColor(85, 114, 128));  painter.drawRoundedRect(rect, cornerRadius, cornerRadius);   painter.setBrush(Qt::lightGray); //drawcaricon(&painter);if (leftArrow)drawLeftArraow(&painter);elsedrawRightArrow(&painter);
}void arrowWidget::drawLeftArraow(QPainter* painter)
{QPoint center(this->width() / 2, this->height() / 2);int arrowWidth = this->width() * 0.2; int arrowHeight = this->height() * 0.2; QPoint left(center.x() - arrowWidth, center.y());    QPoint top(center.x() + arrowWidth / 2, center.y() - arrowHeight); QPoint bottom(center.x() + arrowWidth / 2, center.y() + arrowHeight);QPolygon arrow;arrow << left << top << bottom;painter->drawPolygon(arrow);
}void arrowWidget::drawRightArrow(QPainter* painter)
{QPoint center(this->width() / 2, this->height() / 2);int arrowWidth = this->width() * 0.2; int arrowHeight = this->height() * 0.2;QPoint right(center.x() + arrowWidth, center.y());   QPoint top(center.x() - arrowWidth / 2, center.y() - arrowHeight); QPoint bottom(center.x() - arrowWidth / 2, center.y() + arrowHeight); QPolygon arrow;arrow << right << top << bottom;painter->drawPolygon(arrow);
}

在这里插入图片描述这是效果图,有需要其他操作的可以自己根据实际情况调整。


http://www.ppmy.cn/embedded/116823.html

相关文章

Vue3 Day7-全局组件、指令以及pinia

7.1 全局组件 App.vue <template><div><h2>我是父组件&#xff0c;下面是全局组件的内容</h2><HelloWorld></HelloWorld></div> </template> ​ <script setup> ​ </script> <style scoped></style&g…

git 命令

reset all file to a commit-id &#xff08;退code&#xff09; git reset --hard HEAD^ /commit id only return one change &#xff08;推其中的一笔&#xff09; git revert commit id reback one specific file to latest&#xff08;恢复那…

Linux基础---13三剑客及正则表达式

一.划水阶段 首先我们先来一个三剑客与正则表达式混合使用的简单示例&#xff0c;大致了解是个啥玩意儿。下面我来演示一下如何查询登录失败的ip地址及次数。 1.首先&#xff0c;进入到 /var/log目录下 cd /var/log效果如下 2.最后&#xff0c;输入如下指令即可查看&#xf…

Apollo自动驾驶项目(二:cyber框架分析)

Apollo 的 Cyber 框架 是一个基于消息传递的中间件&#xff0c;用于处理模块之间的通信和数据共享&#xff0c;类似于 ROS&#xff08;Robot Operating System&#xff09;。它是 Apollo 系统的核心框架之一&#xff0c;负责协调自动驾驶软件中不同模块的协同工作。Cyber 框架为…

大数据新视界 --大数据大厂之HBase 在大数据存储中的应用与表结构设计

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

Rust的作用?

在Linux中&#xff0c;Rust可以开发命令行工具&#xff0c;如FD、SD、Ripgep、Bat、EXA、SKIM等。虽然Rust是面向系统编程&#xff0c;但也不妨碍使用Rust写命令行工具&#xff0c;因为Rust具备现代语言特性、无依赖、生成的目标文件小。 在云计算和区块链区域&#xff0c;Rus…

51单片机 - DS18B20实验1-读取温度

上来一张图&#xff0c;明确思路&#xff0c;程序整体裤架如下&#xff0c;通过单总线&#xff0c;单独封装一个.c文件用于单总线的操作&#xff0c;其实&#xff0c;我们可以把点c文件看成一个类操作&#xff0c;其属性就是我们面向对象的函数&#xff0c;也叫方法&#xff0c…

C# .net6 开发数据采集软件(一)

功能&#xff1a; 数据采集&#xff1a;采集任务 数据分析&#xff1a;数据可视化 其他功能&#xff1a;数据上传、数据下拉、软件更新 软件设置&#xff1a;PLC配置、任务配置、软件配置、可视化配置 更多功能&#xff1a;其他软件的入口&#xff0c;或者小工具的使用。比…