qt 5.15.2 主窗体事件及绘制功能
qt 5.15.2 主窗体事件及绘制功能
显示主窗体效果图如下所示:

main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.setFixedWidth(600);w.setFixedHeight(600);w.show();//w.showMaximized(); //最大化显示后,再添加布局部件return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();//void print(QString msg);void DrawBorad();void DrawTipText();void PrintMsg(int x,int y,QString msg);void line(int x1,int y1,int x2,int y2);void circle(int x,int y,int radius);//qt 绘制事件void paintEvent(QPaintEvent *event);//qt mouse eventvoid mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void keyPressEvent(QKeyEvent *event);void wheelEvent(QWheelEvent *event);void resizeEvent(QResizeEvent *event);//void DrawPiece();private:Ui::MainWindow *ui; bool isMousePressed=false;QPoint currentPoint;char Current_piece='O';//初始化char Board_piece[3][3]={{'_','_','_'},{'_','_','o'},{'_','_','x'}};
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qpainter.h>
#include <iostream>
#include <QDebug>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QResizeEvent>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//mouse move event setup must two line OKthis->setMouseTracking(true);ui->centralwidget->setMouseTracking(true);//
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::print(QString msg)
{qDebug()<<msg;//std:cout<<msg.toStdString()<<std::endl;
}void MainWindow::DrawBorad()
{line(0,200,600,200); //cline(0,400,600,400); //dline(200,0,200,600); //aline(400,0,400,600); //b
}void MainWindow::line(int x1,int y1,int x2,int y2)
{QPainter painter(this);painter.drawLine(x1,y1,x2,y2);
}void MainWindow::circle(int x,int y,int radius)
{QPainter painter(this);painter.drawEllipse(QPoint(x,y),radius,radius);
}void MainWindow::DrawTipText()
{QPainter painter(this);//static TCHAR str[64];//_stprintf_s(str,_T("当前棋子类型:%c"),Current_piece);//settextcolor(RGB(255,175,45));//outtextxy(0,0,str);QPen pen;pen.setWidth(2);//设置线宽pen.setStyle(Qt::SolidLine);//样式pen.setColor(QColor(255,0,0));//文字颜色painter.setPen(pen);QString msg=QString::asprintf("当前棋子类型:%c",Current_piece);painter.drawText(10, 15, msg);//文本内容
}void MainWindow::PrintMsg(int x,int y,QString msg)
{QPainter painter(this);//static TCHAR str[64];//_stprintf_s(str,_T("当前棋子类型:%c"),Current_piece);//settextcolor(RGB(255,175,45));//outtextxy(0,0,str);QPen pen;pen.setWidth(2);//设置线宽pen.setStyle(Qt::SolidLine);//样式pen.setColor(QColor(255,45,100));//文字颜色painter.setPen(pen);painter.drawText(x, y, msg);//文本内容
}void MainWindow::DrawPiece()
{for(int i=0;i<3;i++){for(int j=0;j<3;j++){switch(this->Board_piece[i][j]){case 'O':case 'o':this->circle(200*j+100,200*i+100,100);break;case 'x':case 'X':{int x=200*j;int y=200*i;line(x,y,x+200,y+200); //左对角线(x,y)-(x+100,y+100)line(x+200,y,x,y+200); //右对角线(x+100,y)-(x,y+100)}break;case '_':case '-':break;}}}
}//总绘制事件
void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);//反走样painter.setRenderHint(QPainter::Antialiasing, true);//画背景图QString jpgPath="E:\\cwgis_qt\\hsg\\qi_three_son\\images\\china_hdz.jpg";painter.drawPixmap(0,0, 600, 600, QPixmap(jpgPath));//定义画笔QPen pen;pen.setWidth(2);//设置线宽pen.setColor(QColor(222,255,55));//颜色pen.setStyle(Qt::SolidLine);//样式//画刷QBrush brush;brush.setColor(Qt::black);//颜色brush.setStyle(Qt::SolidPattern);//样式//设置画笔画刷painter.setPen(pen);painter.setBrush(brush);//画直线painter.drawLine(50,50,150,50);painter.drawLine(20,50,250,150 );//画矩形painter.drawRect(70,200,100,50);//画椭圆painter.drawEllipse(QPoint(270,150),50,80);//画文字QFont font;font.setFamily("MV Boli");//文字字体font.setPixelSize(20);//文字大小pen.setColor(QColor(255,55,255));//文字颜色painter.setFont(font);painter.setPen(pen);painter.drawText(200, 300, "Qt Creator 12.0.0 (opensource)");//文本内容//this->DrawBorad();this->DrawPiece();//this->DrawTipText();this->PrintMsg(20,30,jpgPath);this->PrintMsg(20,40,qApp->applicationDirPath()); //获取可执行文件所在目录//{QString msg=QString::asprintf("%d,%d",this->currentPoint.x(),this->currentPoint.y());this->PrintMsg(40,580,"(x,y)="+msg);}
}void MainWindow::mousePressEvent(QMouseEvent *event)
{this->isMousePressed=true;print("pressed mouse");this->update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{this->isMousePressed=false;print("unpresse mouse");this->update();
}void MainWindow::mouseMoveEvent(QMouseEvent *event)
{QPoint p=event->pos();this->currentPoint=p;qDebug()<<"pos="<<p; //需要引用#include <QMouseEvent>//QString msg=QString::asprintf("%d,%d",p.x(),p.y());//this->PrintMsg(40,300,"(x,y)="+msg);//qDebug()<<msg;if(this->isMousePressed){QString msg=QString::asprintf("%b",this->isMousePressed);this->PrintMsg(570,40,"isMousePressed="+msg);}else{QString msg=QString::asprintf("%b",this->isMousePressed);this->PrintMsg(570,40,"isMousePressed="+msg);}print("moveing mouse");this->update(); //更新触发重绘事件paintEvent 才能动态显示当前点坐标(x,y)=100,200
}void MainWindow::keyPressEvent(QKeyEvent *event)
{qDebug()<<"key: "<<event->key();this->update();
}
void MainWindow::wheelEvent(QWheelEvent *event)
{qDebug()<<"wheel: "<<event->angleDelta();this->update();
}void MainWindow::resizeEvent(QResizeEvent *event)
{qDebug()<<"resize: "<<event->size();this->update();
}
本blog地址:https://blog.csdn.net/hsg77
相关文章:
qt 5.15.2 主窗体事件及绘制功能
qt 5.15.2 主窗体事件及绘制功能 显示主窗体效果图如下所示: main.cpp #include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.setFixedWidth(600);w.setFixedHeight(6…...
(2)(2.4) TerraRanger Tower/Tower EVO(360度)
文章目录 前言 1 安装传感器并连接 2 通过地面站进行配置 3 参数说明 前言 TeraRanger Tower 可用于在 Loiter 和 AltHold 模式下进行目标规避。传感器的最大可用距离约为 4.5m。 TeraRanger Tower EVO 可用于在 Loiter 和 AltHold 模式下进行目标规避。传感器的最大可用…...
Redis_主从复制、哨兵模式、集群模式详解
Redis的主从复制 为什么Redis要引入主从复制?what? 在这里博主为小伙伴们简单的做下解释,可以了解一下 实际生产环境下,单机的redis服务器是无法满足实际的生产需求的。 第一,单机的redis服务器很容易发生单点故障&am…...
关于神舟-战神TA5NS系统重装问题
加装固态卡在log处无法开机问题 下面是我的步骤 1.按f7选择pe安装系统,然后发现卡在战神log处不转动 2.下载驱动 TA5NS驱动地址 下载RAID驱动(如果没有私信我,我网盘里有),拷到u盘中,然后进入pe系统里面…...
前端大文件上传webuploader(react + umi)
使用WebUploader还可以批量上传文件、支持缩略图等等众多参数选项可设置,以及多个事件方法可调用,你可以随心所欲的定制你要的上传组件。 分片上传 1.什么是分片上传 分片上传,就是将所要上传的文件,按照一定的大小,将…...
人大金仓(kingbase)数据库常用sql命令
一. 字段 1. 添加 alter table book add column book_id varchar not null, book_title varchar(10) default ;2. 删除 alter table book drop book_id, book_title;// 外键时 alter table book drop book_id, book_title cascade;3. 修改类型 alter table book alter colu…...
HashMap相关专题
前置知识:异或运算 异或运算介绍 异或有什么神奇之处(应用)? (1)快速比较两个值 (2)我们可以使用异或来使某些特定的位翻转,因为不管是0或者是1与1做异或将得到原值的相…...
threejs WebGLRenderer 像素比对画布大小的影响
官方文档 - WebGLRenderer .setPixelRatio ( value : number ) : undefined 设置设备像素比。通常用于避免HiDPI设备上绘图模糊 .setSize ( width : Integer, height : Integer, updateStyle : Boolean ) : undefined 将输出canvas的大小调整为(width, height)并考虑设备像素比…...
RocketMQTemplate.send() 与 RocketMQTemplate.syncSend() 方法详解
Apache RocketMQ 是一款强大的分布式消息中间件,与 Spring Boot 集成后,通过 RocketMQTemplate 提供了多种方法来发送消息。其中,send() 和 syncSend() 是两个常用的发送消息方法,本文将深入探讨它们的区别以及详细解释这两个方法…...
波奇学C++:类型转换和IO流
隐式类型转换 int i0; double pi; 强制类型转换 int* pnullptr; int a(int)p; 单参数构造函数支持隐式类型转换 class A { public:A(string a):_a(a){} private:string _a; }; A a("xxxx"); //"xxx" const char* 隐式转换为string 多参数也可以通过{…...
集成开发环境 PyCharm 的安装【侯小啾python基础领航计划 系列(二)】
集成开发环境PyCharm的安装【侯小啾python基础领航计划 系列(二)】 大家好,我是博主侯小啾, 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔…...
Java核心知识点整理大全27-笔记(已完结)
目录 30. 云计算 30.1.1. SaaS 30.1.2. PaaS 30.1.3. IaaS 30.1.4. Docker 30.1.4.1. 概念 30.1.4.2. Namespaces 30.1.4.3. 进程(CLONE_NEWPID 实现的进程隔离) 30.1.4.4. Libnetwork 与网络隔离 30.1.4.5. 资源隔离与 CGroups 30.1.4.6. 镜像与 UnionFS 30.1.4.7.…...
1. 使用poll或epoll创建echo服务器
1. 说明: 此篇博客主要记录一种客户端实现方式,和两种使用poll或者epoll分别创建echo服务器的方式,具体可看代码注释: 2. 相关代码: 2.1 echoClient.cpp #include <iostream> #include <cstdio> #incl…...
【对象数组根据属性排序】
// sort使用的排序方法 // 传入对象数组用于排序的对象的属性,升序/降序 function compare(property, sortType "asc") {debugger// 如果不是 asc,desc,不做下一步比较if (!(sortType "desc" || sortType "asc")) {return;}return function (…...
BACnet I/O模块:楼宇自动化的未来选择
在楼宇自动化领域,BACnet通信协议在确保设备之间无缝高效的数据交换方面发挥着至关重要的作用。该领域使用广泛的协议是BACnet。它使传感器、执行器和控制器等设备能够相互通信,从而促进工业过程的自动化。 BACNET介绍 BACnet是专门为楼宇自动化和控制系…...
android项目实战之使用框架 集成多图片、视频的上传
效果图 实现方式,本功能使用PictureSelector 第三方库 。作者项目地址:https://github.com/LuckSiege/PictureSelector 1. builder.gradle 增加 implementation io.github.lucksiege:pictureselector:v3.11.1implementation com.tbruyelle.rxpermissio…...
MyBatis查询优化:枚举在条件构建中的妙用
🚀 作者主页: 有来技术 🔥 开源项目: youlai-mall 🍃 vue3-element-admin 🍃 youlai-boot 🌺 仓库主页: Gitee 💫 Github 💫 GitCode 💖 欢迎点赞…...
Isaac Sim教程04 Isaac Sim的高级使用
Isaac Sim 高级使用 版权信息 Copyright 2023 Herman YeAuromix. All rights reserved.This course and all of its associated content, including but not limited to text, images, videos, and any other materials, are protected by copyright law. The author holds…...
《数据结构、算法与应用C++语言描述》-线索二叉树的定义与C++实现
_23Threaded BinaryTree 可编译运行代码见:GIithub::Data-Structures-Algorithms-and-Applications/_24Threaded_BinaryTree 线索二叉树定义 在普通二叉树中,有很多nullptr指针被浪费了,可以将其利用起来。 首先我们要来看看这空指针有多少…...
删除误提交的 git commit
背景描述 某次的意外 commit 中误将密码写到代码中并且 push 到了 remote repo 里面, 本文将围绕这个场景讨论如何弥补. 模拟误提交操作 在 Gitee 创建一个新的 Repo, clone 到本地 git clone https://gitee.com/lpwm/myrepo.git创建两个文件, commit 后 push 到 remote 作…...
基于uniapp+WebSocket实现聊天对话、消息监听、消息推送、聊天室等功能,多端兼容
基于 UniApp + WebSocket实现多端兼容的实时通讯系统,涵盖WebSocket连接建立、消息收发机制、多端兼容性配置、消息实时监听等功能,适配微信小程序、H5、Android、iOS等终端 目录 技术选型分析WebSocket协议优势UniApp跨平台特性WebSocket 基础实现连接管理消息收发连接…...
linux arm系统烧录
1、打开瑞芯微程序 2、按住linux arm 的 recover按键 插入电源 3、当瑞芯微检测到有设备 4、松开recover按键 5、选择升级固件 6、点击固件选择本地刷机的linux arm 镜像 7、点击升级 (忘了有没有这步了 估计有) 刷机程序 和 镜像 就不提供了。要刷的时…...
关于 WASM:1. WASM 基础原理
一、WASM 简介 1.1 WebAssembly 是什么? WebAssembly(WASM) 是一种能在现代浏览器中高效运行的二进制指令格式,它不是传统的编程语言,而是一种 低级字节码格式,可由高级语言(如 C、C、Rust&am…...
Swagger和OpenApi的前世今生
Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章,二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑: 🔄 一、起源与初创期:Swagger的诞生(2010-2014) 核心…...
云原生安全实战:API网关Kong的鉴权与限流详解
🔥「炎码工坊」技术弹药已装填! 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关(API Gateway) API网关是微服务架构中的核心组件,负责统一管理所有API的流量入口。它像一座…...
如何更改默认 Crontab 编辑器 ?
在 Linux 领域中,crontab 是您可能经常遇到的一个术语。这个实用程序在类 unix 操作系统上可用,用于调度在预定义时间和间隔自动执行的任务。这对管理员和高级用户非常有益,允许他们自动执行各种系统任务。 编辑 Crontab 文件通常使用文本编…...
C#学习第29天:表达式树(Expression Trees)
目录 什么是表达式树? 核心概念 1.表达式树的构建 2. 表达式树与Lambda表达式 3.解析和访问表达式树 4.动态条件查询 表达式树的优势 1.动态构建查询 2.LINQ 提供程序支持: 3.性能优化 4.元数据处理 5.代码转换和重写 适用场景 代码复杂性…...
Python 实现 Web 静态服务器(HTTP 协议)
目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1)下载安装包2)配置环境变量3)安装镜像4)node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1)使用 http-server2)详解 …...
Java求职者面试指南:Spring、Spring Boot、Spring MVC与MyBatis技术解析
Java求职者面试指南:Spring、Spring Boot、Spring MVC与MyBatis技术解析 一、第一轮基础概念问题 1. Spring框架的核心容器是什么?它的作用是什么? Spring框架的核心容器是IoC(控制反转)容器。它的主要作用是管理对…...
职坐标物联网全栈开发全流程解析
物联网全栈开发涵盖从物理设备到上层应用的完整技术链路,其核心流程可归纳为四大模块:感知层数据采集、网络层协议交互、平台层资源管理及应用层功能实现。每个模块的技术选型与实现方式直接影响系统性能与扩展性,例如传感器选型需平衡精度与…...
